انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

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:
Score? 75
Very good
🐍 Open the code in Robo Python ↗
📌 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.

🐍 The grade
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:
Mark: 81
B
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A number: positive, negative or zero.

👀 Show the solution

When the program asks, we type: -4.

🐍 Solution
n = int(input("n: "))
if n > 0:
    print("positive")
elif n < 0:
    print("negative")
else:
    print("zero")
🖥️ The output in Robo Python:
n: -4
negative
🐍 Open the code in Robo Python ↗

Result: negative

Medium

The traffic light: ask for the colour and print what to do (go / wait / stop).

👀 Show the solution

When the program asks, we type: yellow.

🐍 Solution
light = input("Light: ")
if light == "green":
    print("go")
elif light == "yellow":
    print("wait")
else:
    print("stop")
🖥️ The output in Robo Python:
Light: yellow
wait
🐍 Open the code in Robo Python ↗

Result: wait

Hard

The day from its number: 1 = Saturday … 7 = Friday.

👀 Show the solution

When the program asks, we type: 4.

🐍 Solution
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:
Day number: 4
Tuesday
🐍 Open the code in Robo Python ↗

Result: Tuesday

Finished the lesson? Mark it so your progress counts.