انتقل للمحتوى
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

if and else

Section 3: ConditionsLesson 3.1 · ⏱ 8 min

if asks a question: if it is true, it runs the indented lines under it. And else is for everything else. Notice the colon : and the indent of four spaces.

age = int(input("Age? "))
if age >= 10:
    print("You can join the robotics team!")
else:
    print("Wait a little and join us")
🖥️ The output in Robo Python:
Age? 12
You can join the robotics team!
🐍 Open the code in Robo Python ↗
  • == equals, and != does not equal.
  • > bigger, < smaller, and >= bigger or equal.

📌 Another example: even or odd

The remainder when dividing by 2: zero = even.

When the program asks, we type: 7.

🐍 Even or odd
n = int(input("A number: "))
if n % 2 == 0:
    print(n, "is even")
else:
    print(n, "is odd")
🖥️ The output in Robo Python:
A number: 7
7 is odd
🐍 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

Ask for the age: 18 or more = adult, otherwise child.

👀 Show the solution

When the program asks, we type: 12.

🐍 Solution
age = int(input("Age: "))
if age >= 18:
    print("adult")
else:
    print("child")
🖥️ The output in Robo Python:
Age: 12
child
🐍 Open the code in Robo Python ↗

Result: child

Medium

Ask for the temperature: above 30 = hot, otherwise nice.

👀 Show the solution

When the program asks, we type: 36.

🐍 Solution
t = int(input("Temperature: "))
if t > 30:
    print("hot")
else:
    print("nice")
🖥️ The output in Robo Python:
Temperature: 36
hot
🐍 Open the code in Robo Python ↗

Result: hot

Hard

Ask for two numbers, and print the bigger one.

👀 Show the solution

When the program asks, we type: 4, then 9.

🐍 Solution
a = int(input("a: "))
b = int(input("b: "))
if a > b:
    print("bigger:", a)
else:
    print("bigger:", b)
🖥️ The output in Robo Python:
a: 4
b: 9
bigger: 9
🐍 Open the code in Robo Python ↗

Result: bigger: 9

Finished the lesson? Mark it so your progress counts.