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:
🐍 Open the code in Robo Python ↗What is your name? Omar How old are you? 10 Hello Omar Next year you will be 11
🖥️ 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.
name = input("Your name? ")
color = input("Favourite colour? ")
print(name, "likes", color)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Your name? Mona Favourite colour? green Mona likes green
✍️ 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
Ask for the city, and print: Welcome to … .
👀 Show the solution🙈 Hide the solution
When the program asks, we type: Luxor.
city = input("Your city? ")
print("Welcome to", city)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Your city? Luxor Welcome to Luxor
Result: Welcome to Luxor
Medium
Ask for the age (int), and print the age in 10 years.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 11.
age = int(input("Your age? "))
print("In 10 years you will be", age + 10)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Your age? 11 In 10 years you will be 21
Result: In 10 years you will be 21
Hard
Ask for a number, and print its double and its square.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 7.
n = int(input("A number? "))
print("double:", n * 2)
print("square:", n * n)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗A number? 7 double: 14 square: 49
Result: double: 14 and square: 49

