โ†Previous page

09SUPERPOWERS ยท INPUT

PyBot asks, you answer.

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

Questions and answers everywhere.

Someone asks, you answer, and they remember your answer.

The ice cream shop

The seller asks: Which flavor? You say: mango. They remember it to fill your cone.

A new game

The game asks your name before you play. Then it says: Hi, Ana!

The cash machine

It asks how much money. You type 20. It reads text, then turns it into a number.

LOOK FIRST

Ask, wait, keep.

input() asks

input("Your name? ") shows the question and waits for an answer.

The answer goes in a box

name = input("Your name? ") keeps the answer in the box name.

Answers are always text

Even if you type 7, input() gives back the text "7". To do math, change it with int().

STEP BY STEP

From answer to number.

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 10

RUN IT

PyBot asks your name.

Guess first. Then run real Python and check.

1

Guess the result

name = input("Your name? ") print("Hi,", name) print("Nice to meet you!")

The answers box says Ana. What does the second line show?

hello.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide โ™ฅ

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

Ask and answer.

Try each tiny question. A wrong answer becomes something to review.

0Completed
9Not tried
0Review
01

The ice cream question

PyBot wants to ask: Which flavor? and keep the answer.

Which line does it?

02

Python waits

When Python reaches input(), what happens?

03

Always text

age = input("Age? ")

You answer 9.

What does the box age keep?

04

Glued, not added

a = input("Number? ") b = input("Number? ") print(a + b)

You answer 2, then 3.

What does print show?

05

Make it a number

Which one turns the text "8" into the number 8?

06

Now math works

age = int(input("Age? ")) print(age + 1)

You answer 9.

What does print show?

07

Letters are not digits

n = int(input("Number? "))

You answer ten, with letters.

What happens?

08

Fix PyBot's code

PyBot wants to double your number, but it shows 44. The answers box says 4. Make it show the goal.

Answers box
4
Goal output
Pick a number: 4
8
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

input() asks, int() makes a number.

Every answer arrives as text. When you need to count or do math with it, wrap it in int().