▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
Getting input from the user
Ask questions and convert answers into numbers.
🎬 Video version coming soon — read it now
input() pauses the program, waits for the user to type something, and gives you what they typed.
In Colab a text box appears under the cell. Type, then press Enter.
input() always gives you text
This is the number-one beginner trap:
age holds the text "20", not the number 20. Convert it:
int()for whole numbersfloat()for decimals, such as prices or weightsstr()to turn a number back into text
A small interactive program
What if the user types rubbish?
If someone types twenty when you ask for a number, int() crashes with a ValueError. That's normal for now. In the Functions & Clean Code course you'll learn try/except to handle it politely.
"Your age: ", so the user's typing doesn't stick to your text.In one line
input() reads what the user types as text, so wrap it in int() or float() when you need a number.
Share with a friend