- What is the Python symbol for line continuation?
Ans: Backslash. For example:
sum = x1 + x2 + x3 + \
x4 + x5 + x6
No spaces or tabs are allowed on the line after the backslash.
- Construct the variable trace and predict the output for these examples:
Trace3 Trace4
Check your answer by running the script in each case.
- Rewrite the last line of this script using an F-string:
person1 = "Bill"
person2 = "Dan"
amt = 300.0
print(person1, " owes ", person2, " $", str(amt), ".")
Ans:
print(f"{person1} owes {person2} ${amt}.")
- What is printed when the int values 2 and 3 are input from the keyboard? Construct the variable trace to
help you predict the output.
x = int(input("Enter x value: "))
y = int(input("Enter y value: "))
x = y
y = x
print(f"{x} {y}")
Ans:
Variable Trace: x y
-----+-----
2 3
3 3
Output:
3 3
When you execute x = y, you lose the value of
2 in x.
- Improve the script in Exercise 4 to successfully swap the values of
x and y?
x = int(input("Enter x value: "))
y = int(input("Enter y value: "))
temp = y
y = x
x = temp
print(f"{x} {y}")
# Ans:
# Variable Trace: Output: 3 2
x y temp
-----+-----+-----
2 3 3
2 2 3
3 2 3
You could also use this
special Python command x,y = y,x that assigns
x and y new values at the same time:
x = int(input("Enter x value: "))
y = int(input("Enter y value: "))
y, x = x, y
print(f"{x} {y}")
# Output: 3 2
- Find the errors in this script:
// TipCalculator Script
amt = input("Enter amount of check: ')
tip = amt + percent
percent = input(f"Enter tip percentage: "
if tip > 1.00
tip = amt + percentage
elif:
tip = 1.00
print(f"Tip: ${tip}.")
# Corrected Script:
amt = float(input("Enter amount of check: "))
percent = float(input("Enter tip percentage: "))
tip = amt * percent / 100
if tip < 1.00:
tip = 1.00
print(f"Tip: ${tip}.")
- An infinite loop is a loop that never terminates, for example:
while True:
print("I am in an infinite loop.")
Why are each of these infinite loops?
# Loop 1:
n = 0
while n <= 100:
print(n)
# Ans: There is no n = n + 1 statement to change
# the value of n.
# Loop 2:
while n <= 100:
n = 0
print(n)
n = n + 1
# Ans: the initialization n = 0 is inside the loop, so the
value of n is reset to 0 each time the loop executes.
# Loop 3:
n = 0
while n <= 100:
print(n)
n = n + 1
Ans: The iteration statement n = n + 1 is not indented, so it is not
in the while loop. Therefore n never changes.
- Translate these lines from the pseudocode for Project 2b:
set human score to 0.0.
set computer score to 0.0.
while true
input human play with prompt.
choose random number from 0, 1, or 2.
if random number is 0
Set computer play to rock.
else if random number is 1
Set computer play to paper.
else
Set computer play to scissors.
end
if human plays rock and computer plays paper
Output paper covers scissors, computer wins.
Add 1.0 to computer's score.
end
end
# Python translation:
human_score = 0
computer_score = 0
while True:
human_play = input("Enter human play: ")
random_number = random.randrange(0, 3)
if random_number == 0:
computer_play = "rock"
elif random_number == 1:
computer_play = "paper"
else:
computer_play = "scissors"
if human_play == "rock" and computer_play == "paper":
print("Rock covers paper")
print("Computer wins.")
computer_score = computer_score + 1
- The following while loop prints 5 stars (n ranges from 0 to
4):
n = 0
while n < 5:
print("*")
n = n + 1
- This for loop is called a definite loop:
for i in range(0, 5):
print("*")
- range(0, 5) consists of the values 0, 1, 2, 3, 4.
- This for loop is called a definite loop because it executes a
definite number of times, exactly 5.
- The general form of a range:
range(start, end + 1)
It consists of the values start, start + 1, ... , end - 1, end.
- A range can also include a step size:
range(start, end + 1, step)
It consists of the values start, start + step, start + 2 * step, ... , end
- For example:
range(1, 10, 2)
consists of the values 1, 3, 5, 7, 9.
- Practice Problems: Write definite loops that print
these sequences:
a. 101 102 103 104 105
Ans:
for(i in range(101, 106):
print(i, end=" ")
b. 5 5 6 6 7 7 8 8 9 9
Ans:
for(i in range(5, 10)):
print(i, end=" ")
print(i, end=" ")
or
for(i in range(5, 10)):
print(i, i, end=" ")
c. 3 6 9 12 15 18
# Ans:
for(i in range(3, 19, 3):
print(i)
d. 1 2 4 8 16 32 64 128
# Ans:
n = 1
for i in range(1, 9):
print(n, end=" ")
n = n * 2
- Look at the DeafGrandma Example.