elif: more than one case
Section 3: ConditionsLesson 3.2 · ⏱ 8 min
score = int(input("Score? "))
if score >= 90:
print("Excellent")
elif score >= 70:
print("Very good")
elif score >= 50:
print("Good")
else:
print("Try again")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Score? 75 Very good
📌 Note
Python checks from top to bottom, and runs only the first condition that is true.
📌 Another example: the grade
A mark out of 100 becomes a letter: A, B, C and D.
When the program asks, we type: 81.
mark = int(input("Mark: "))
if mark >= 90:
print("A")
elif mark >= 75:
print("B")
elif mark >= 50:
print("C")
else:
print("D")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Mark: 81 B
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
A number: positive, negative or zero.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: -4.
n = int(input("n: "))
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗n: -4 negative
Result: negative
Medium
The traffic light: ask for the colour and print what to do (go / wait / stop).
👀 Show the solution🙈 Hide the solution
When the program asks, we type: yellow.
light = input("Light: ")
if light == "green":
print("go")
elif light == "yellow":
print("wait")
else:
print("stop")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Light: yellow wait
Result: wait
Hard
The day from its number: 1 = Saturday … 7 = Friday.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 4.
d = int(input("Day number: "))
if d == 1:
print("Saturday")
elif d == 2:
print("Sunday")
elif d == 3:
print("Monday")
elif d == 4:
print("Tuesday")
elif d == 5:
print("Wednesday")
elif d == 6:
print("Thursday")
else:
print("Friday")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Day number: 4 Tuesday
Result: Tuesday

