Learning Goals
3 minBy the end of this lesson you can:
- Ask the user a question with
input()and store their answer. - Remember that
input()always gives back a string. - Convert text answers into numbers using
int()andfloat().
Warm-Up
5 minIn our Mad Libs game last lesson, every silly word lived at the top of the file. To play with a friend, you had to edit the file. That isn't really a game — yet.
Quick thought puzzle: how would your Mad Libs change if your friend could just type their words straight into the program when they wanted to play?
Today's tool — input() — turns your scripts into proper conversations. The same skill powers chatbots, calculators, and adventure games.
New Concept · Two-Way Conversations
12 minPart 1 · The input() function
input() pauses your program and waits for the user to type something and press Enter. Whatever they type lands in a variable as a string.
name = input("What is your name? ") print("Hello,", name)
The text inside input("...") is the prompt — the question shown to the user.
Always end your prompt with a space, so the answer doesn't smush against the question. "Name? " is friendlier than "Name?".
Part 2 · input() always gives back a string
Even if the user types only digits, Python stores them as a string:
age = input("How old are you? ") print(age + 1)
Python complains because "12" is a string. We can't add a string and a number with +.
Part 3 · Convert with int() and float()
Two tiny helpers turn numeric strings into real numbers:
int("12")→12— for whole numbersfloat("3.14")→3.14— for decimals
The classic two-step pattern:
age_text = input("How old are you? ") age = int(age_text) print("Next year you'll be", age + 1)
Or, the same thing in one tidy line — call int() right around input():
age = int(input("How old are you? ")) print("Next year you'll be", age + 1)
Part 4 · Where to type your answer
Different editors hand input() answers in slightly different places:
- VS Code: click the green ▶ Run Python File button (top-right). Your prompt appears in the Terminal panel at the bottom — type there and press Enter.
- online-python.com: click the Input tab on the right, type each answer on its own line first, then click Run.
If the user types something that isn't a valid number, int(...) crashes. We will learn a safe way to handle this in Level 2 — for now, trust the user to type correctly.
Worked Example · A Personalised Greeter
12 minWe will build a tiny program that asks two questions and answers back. Save this as greeter.py.
Code
# greeter.py — ask the user some questions, then reply # 1. Ask for a name (a string is fine — no conversion needed) name = input("What is your name? ") # 2. Ask for age, then convert to int age_text = input("How old are you? ") age = int(age_text) # 3. Build a friendly response print() print("Selamat datang,", name + "!") print("Next year you'll be", age + 1, "— exciting!") # 4. A little maths on the answer months_alive = age * 12 print("You have been alive for roughly", months_alive, "months.")
When you run it, the terminal looks like this (lines you type are bold):
Output
What is your name? <strong>Aisyah</strong> How old are you? <strong>12</strong> Selamat datang, Aisyah! Next year you'll be 13 — exciting! You have been alive for roughly 144 months.
The compact version
Once you're comfortable, you can fold the conversion into one line:
age = int(input("How old are you? ")) print("In ten years you'll be", age + 10)
Both versions do the same thing. The two-step version is easier to read; the compact version is shorter. Pick whichever helps you understand the code.
Asking for a decimal
height = float(input("Your height in metres? ")) print("Half your height is", height / 2, "metres.")
Try It Yourself
13 minType each task into your editor. Run it. Fix any typos before moving on.
Ask the user "What did you say?" and store their answer in a variable. Then print "You said: " followed by the answer.
Hint
Two lines of code: one input(...), one print(...). No conversion needed because we're just echoing text.
Ask the user for a number. Convert it with int(). Print the number, then print double the number.
Hint
n = int(input("A number? ")), then print(n * 2).
Re-do this short Mad Libs sentence — but ask the user for the missing words instead of hard-coding them:
"Today, " + name + " ate " + str(count) + " pieces of " + food + " in " + place + "."
Use input() for name, food, and place. Use int(input(...)) for count.
Hint
Four input() calls — three plain, one wrapped in int(...). Then build the sentence with + and str() as in PY-L1-06.
Mini-Challenge · Mamak Order Calculator
8 minBuild a tiny mamak order calculator that talks to the user.
Your file must include:
- An
input()for the customer'sname(no conversion). - An
input()for how manyroti_canaithey want — convert withint(). Each roti canai costs RM 2.00. - An
input()for how manyteh_tarikthey want — convert withint(). Each teh tarik costs RM 2.50. - Calculate the total in a variable.
- Print a friendly receipt: greeting by name, item lines, and the total in
RM.
Stretch goal. Also ask "How many friends are sharing the bill?" and print how much each person pays (total / friends).
Show one possible solution
# mamak_order.py — a friendly mamak calculator # 1. Ask the customer name = input("What's your name? ") roti_qty = int(input("How many roti canai? ")) teh_qty = int(input("How many teh tarik? ")) # 2. Prices and total roti_price = 2.00 teh_price = 2.50 total = roti_qty * roti_price + teh_qty * teh_price # 3. Receipt print() print("=" * 32) print(" Mamak Code — Order Receipt") print("=" * 32) print("Customer: ", name) print("Roti canai: ", roti_qty, "x RM", roti_price) print("Teh tarik: ", teh_qty, "x RM", teh_price) print("-" * 32) print("TOTAL: RM", total) print("=" * 32) # Stretch — split between friends friends = int(input("How many friends are sharing? ")) each_pays = total / friends print("Each person pays: RM", each_pays)
Recap
3 minOur programs are no longer one-way. With input() we can ask, and with int() and float() we can do real maths on the answers.
Vocabulary Card
- input()
- Pauses the program and reads a line of text from the user.
- prompt
- The little question shown by
input(). End it with a space. - int()
- Turns a numeric string like
"12"into the int12. - float()
- Turns a decimal string like
"3.14"into the float3.14.
Homework
4 minCreate a new file called calc.py — a tiny personal calculator.
Rules:
- Ask the user three questions: their
name, theircurrent_age, and atarget_age(like 100). - Convert both ages with
int(). - Calculate
years_to_go = target_age - current_age. - Print a friendly multi-line reply that names the user and shows the result.
- Stretch — also calculate
years_in_school = current_age - 5(assuming Standard 1 starts at age 7, kindergarten at 5) and print it.
Bring it to the next lesson — we'll run each other's calculators!
Sample · calc.py
# calc.py — a tiny personal calculator # 1. Questions for the user name = input("What is your name? ") current_age = int(input("How old are you now? ")) target_age = int(input("Pick a target age (e.g., 100): ")) # 2. Maths years_to_go = target_age - current_age # 3. Reply print() print("Hi", name + "!") print("You have", years_to_go, "years to go until you turn", target_age, "years old.") # Stretch — years spent in school (assuming kindergarten started at age 5) years_in_school = current_age - 5 print("You have spent about", years_in_school, "years in school so far.")
Yours can ask any extra questions you like. As long as you use input() three times and int() for the two ages, you've nailed it.