🐍 Python for kids · Class 5 · Lesson 4 of 18 · Free
Getting User Input
⏱ About 25 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Ask the user a question with input()
- Turn typed answers into numbers with int() and float()
- Build a small program that asks and then calculates
A shopkeeper asks, "What do you want, and how many?" Then he works out your bill. Until now, your programs only talked. Today they will listen, ask questions and use your answers.
How does input() work in Python?
input() shows a question and waits. The program pauses until the user types an answer and presses Enter.
Whatever the user types comes back to your program. You store it in a variable to use it later.
On this page, the Inputs box holds sample answers for each input(). Change them and press Run to try your own.
▶name = input("What is your name? ")2print("Namaste,", name)3print("Welcome to Python class,", name)
No variables yet.
▌✏️ Change this code and run it yourself
What is your name? Ananya Namaste, Ananya Welcome to Python class, Ananya
💡 Leave a space before the closing quote of your question. Then the answer does not stick to the question mark.
Why does input() always give text?
input() cannot tell what kind of answer you want. So it always gives back a string, even when you type digits.
type() proves it. The user types 10, but Python stores the text "10".
Text cannot do maths. So we need a way to turn that text into a real number.
How old are you? 10 <class 'str'>
How do int() and float() turn text into numbers?
int() changes text like "10" into the whole number 10. float() changes text like "4.5" into the decimal 4.5.
You can wrap input() inside int() on the same line. Python first asks the question, then converts the answer.
Now the age is a number, so Python can add to it.
▶age = int(input("How old are you? "))2next_year = age + 13print("Next year you will be", next_year)4print("In ten years you will be", age + 10)
No variables yet.
▌✏️ Change this code and run it yourself
How old are you? 10 Next year you will be 11 In ten years you will be 20
When should I use float() instead of int()?
Use int() for things you count in whole numbers: age, marks, number of pencils.
Use float() when the answer may have a decimal point: height, weight, price with paise.
If you use int() and the user types 1.45, Python shows an error. float() accepts both 1.45 and 2.
Your height in metres: 1.45 Your height in centimetres is 145.0
How do I write a good question for input()?
The user cannot see your code. They only see the question, so make it clear.
Say what you want and give the unit, like rupees, km or kg. Then nobody types the wrong kind of answer.
A short example in brackets also helps. It shows the user exactly what to type.
Distance to school in km (e.g. 3.5): 3.5 Two trips a day is 7.0 km Six school days is 42.0 km
💡 Compare "Enter: " with "Your weight in kg: ". The second one leaves no doubt.
How can my program ask several questions?
A program can ask as many questions as it needs. Each input() waits for its own answer.
A good plan has three steps: ask, calculate, then show the result. Programmers call this input, process, output.
Here, a tiffin stall works out a bill from three answers.
▶item = input("What would you like? ")2price = int(input("Price of one plate: "))3plates = int(input("How many plates? "))4bill = price * plates5print(plates, "plates of", item, "cost", bill, "rupees")
No variables yet.
▌✏️ Change this code and run it yourself
What would you like? idli Price of one plate: 30 How many plates? 3 3 plates of idli cost 90 rupees
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
x = int("7")
print(x + 3)Question 2. What does this print?
answer = "25" print(type(answer))
Question 3. What does this print?
weight = float("2.5")
print(weight * 2)🧩 Your challenge
Make an age explorer. Ask the user for their name and their age in years. Then print how many months old they are and about how many days old they are (use 365 days in a year).
What is your name? Kabir How old are you? 10 Kabir you are about 120 months old. That is about 3650 days!
⚠️ Common mistakes (and how to fix them)
❌ Wrong
age = input("Age: ")
print(age + 1)TypeError: can only concatenate str (not "int") to str
✅ Right
age = int(input("Age: "))
print(age + 1)input() gives text, and Python cannot add text and a number. Wrap it in int() first.
❌ Wrong
price = int(input("Price: "))
print(price * 2)ValueError: invalid literal for int() with base 10: '12.50'
✅ Right
price = float(input("Price: "))
print(price * 2)int() only understands whole numbers. Use float() when the answer may have a decimal point.
❌ Wrong
a = input("First number: ")
b = input("Second number: ")
print(a + b)No error – but 5 and 3 give 53 instead of 8.
✅ Right
a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)Both answers are text, so + joins them side by side. Convert them to numbers to add.
📌 Remember
- input() shows a question, waits, and gives back what the user types.
- input() always gives a string, even when the user types digits.
- int() turns text into a whole number; float() turns text into a decimal number.
- Write int(input("...")) to ask and convert in one line.
- Plan programs as input, process, output: ask, calculate, show.
Finished the quiz and the challenge?
Questions students ask
What does input() do in Python?
input() shows a message, waits for the user to type something and press Enter, and gives back what they typed as a string.
Why can't I add numbers from input() in Python?
Because input() gives text. "5" + "3" joins to "53". Use int() or float() to turn the text into numbers first.
What is the difference between int() and float()?
int() makes a whole number, like 12. float() makes a decimal number, like 12.5. int() gives an error if the text has a decimal point.
How do I type my own answers on this page?
Each example has an Inputs box with sample answers, one per line. Type your own answers there and press Run again.