Workshop
Quiz
1. What comparison should you use to check whether the value stored in the z variable is greater
than or equal to 10?
a. >
b. <
c. >=
d. ==
2. How would you write the if statement to display a message only if the value stored in the z
variable is between 10 and 20 (not including those values)?
3. How would you write the if statement to give a game player status messages if a guess falls
within 5, 10, or 15 of the actual value?
Answers
1. c. A common mistake in writing conditions is to forget that the greater and less-than symbols
don’t include the specified number!
2. You can nest the variable between to numbers using greater-than or less-than symbols:
Click here to view code image
if 10 < z < 20: print("This is the message")
3. You can use the elif statement to add additional checks for a range of values. It’s important to
remember to check smaller ranges first, as the larger ranges will include the smaller ranges:
Click here to view code image
if (z == answer): print("Correct, you guessed the answer!")
elif (z > answer – 5) or (z < answer + 5): print("You're within 5 of the
answer")
elif (z > answer – 10) or (z < answer + 10): print("You're within 10 of the
answer")
elif (z > answer – 15) or (z < answer + 15): print("You're within 15 of the
answer")