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

input: ask the user

Section 2: Variables and inputLesson 2.3 · ⏱ 8 min

input asks the user and waits for the answer. The answer always comes as text, so if we want it as a number we turn it with int().

name = input("What is your name? ")
year = int(input("How old are you? "))
print("Hello", name)
print("Next year you will be", year + 1)
🖥️ The output in Robo Python:
What is your name? Omar
How old are you? 10
Hello Omar
Next year you will be 11
🐍 Open the code in Robo Python ↗
🖥️ What happens on the stage?
In this example the user typed Omar, then 10.

📌 Another example: two questions

We ask for the name and the colour, and answer with a sentence.

When the program asks, we type: Mona, then green.

🐍 Two questions
name = input("Your name? ")
color = input("Favourite colour? ")
print(name, "likes", color)
🖥️ The output in Robo Python:
Your name? Mona
Favourite colour? green
Mona likes green
🐍 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 city, and print: Welcome to … .

👀 Show the solution

When the program asks, we type: Luxor.

🐍 Solution
city = input("Your city? ")
print("Welcome to", city)
🖥️ The output in Robo Python:
Your city? Luxor
Welcome to Luxor
🐍 Open the code in Robo Python ↗

Result: Welcome to Luxor

Medium

Ask for the age (int), and print the age in 10 years.

👀 Show the solution

When the program asks, we type: 11.

🐍 Solution
age = int(input("Your age? "))
print("In 10 years you will be", age + 10)
🖥️ The output in Robo Python:
Your age? 11
In 10 years you will be 21
🐍 Open the code in Robo Python ↗

Result: In 10 years you will be 21

Hard

Ask for a number, and print its double and its square.

👀 Show the solution

When the program asks, we type: 7.

🐍 Solution
n = int(input("A number? "))
print("double:", n * 2)
print("square:", n * n)
🖥️ The output in Robo Python:
A number? 7
double: 14
square: 49
🐍 Open the code in Robo Python ↗

Result: double: 14 and square: 49

Finished the lesson? Mark it so your progress counts.