The ice cream shop
The seller asks: Which flavor? You say: mango. They remember it to fill your cone.
09SUPERPOWERS ยท INPUT
Until now, PyBot only talked. With input(), PyBot asks a question and waits for your answer. The answer goes into a box.
IN REAL LIFE
Someone asks, you answer, and they remember your answer.
The seller asks: Which flavor? You say: mango. They remember it to fill your cone.
The game asks your name before you play. Then it says: Hi, Ana!
It asks how much money. You type 20. It reads text, then turns it into a number.
LOOK FIRST
input("Your name? ") shows the question and waits for an answer.
name = input("Your name? ") keeps the answer in the box name.
Even if you type 7, input() gives back the text "7". To do math, change it with int().
STEP BY STEP
int() turns text with digits into a real number.
age = input("How old are you? ")PyBot asks. You answer 9. The box age keeps the text "9".age = int(age)int() turns "9" into the number 9.next_year = age + 1Now math works: 9 + 1 = 10.print("Next year you will be", next_year)Shows: Next year you will be 10RUN IT
Guess first. Then run real Python and check.
name = input("Your name? ")
print("Hi,", name)
print("Nice to meet you!")The answers box says Ana. What does the second line show?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Write your own name in the answers box and run it again. Then add a second question, like your favorite color, and a second answer line. Each input() takes the next line.
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
Which line does it?
When Python reaches input(), what happens?
age = input("Age? ")You answer 9.
What does the box age keep?
a = input("Number? ")
b = input("Number? ")
print(a + b)You answer 2, then 3.
What does print show?
Which one turns the text "8" into the number 8?
age = int(input("Age? "))
print(age + 1)You answer 9.
What does print show?
n = int(input("Number? "))You answer ten, with letters.
What happens?
PyBot wants to double your number, but it shows 44. The answers box says 4. Make it show the goal.
4Goal output
Pick a number: 4 8OUTPUT
Want a fresh start? Clear only this page.
Every answer arrives as text. When you need to count or do math with it, wrap it in int().