Ever wondered how video games ask for your character's name, or how websites know your age? The input() command is Python's superpower that lets programs pause, listen to what you type, and use that information in real-time!
Words to own
- input()
- A Python command that pauses your program and waits for the user to type something and press Enter.
- Prompt
- The helpful message shown to the user inside the input() brackets so they know what they are supposed to type.
- String
- A Python data type used for storing text, made of letters, words, or characters inside quotation marks.
- Integer
- A whole number with no decimals, like 5, -12, or 100.
- Type conversion
- The process of changing information from one data type to another, such as converting text into a number.
- User
- The person who is currently running, typing into, and interacting with your computer program.
The Computer Learns to Listen
Up until now, every program you have written has been a one-way street. The computer did all the talking by printing text on the screen, and you just watched. The input() command changes everything. It turns your program into an active listener.
When Python runs an input() command, it stops everything. It waits patiently for the person using the computer to type something and hit the Enter key. Once they press Enter, Python grabs whatever they typed and delivers it right back to your code to use.
- Step 1
1. The Prompt
Python prints 'Age: ' to the screen and pauses the program.
- Step 2
2. User Input
The user types '12' on their keyboard and hits the Enter key.
- Step 3
3. String Delivery
The input() function captures '12' as text (a string, like '12').
- Step 4
4. Conversion
The int() function takes '12' and converts it into the actual integer 12.
- Step 5
5. Storage
Python stores the real number 12 in the variable user_age, ready for calculations.
What is a Prompt?
If you just write input() on its own, Python will pause, but the screen will be completely blank except for a blinking cursor. The user won't have any idea what they are supposed to do!
To fix this, we always put a question or instructions inside the brackets. This text is called the prompt. It acts as a guide to tell the user exactly what to enter.
name = input("What is your name? ")
print("Kia ora,", name)The Secret Rule of input()
There is one massive rule about input() that trips up almost every single beginner programmer. It is the most common bug in early code.
The rule is: input() ALWAYS returns whatever you type as text (called a string). It does not matter if you type a word, a letter, or a number like 12. Python sees it as a text string inside quotation marks. Because it is text, Python cannot do any maths with it yet!
# This looks correct, but it will cause an error!
age = input("How old are you? ")
next_year = age + 1
# Python gets confused adding a number to textThe Fix: Type Conversion
To perform calculations with a user's typed response, we must translate their typed text into a real mathematical number. This is known as type conversion.
We can wrap our input() inside the int() function. This tells Python to immediately convert the typed string into a real whole number (an integer) as soon as the user presses Enter.
age = int(input("How old are you? "))
next_year = age + 1
print("Next year you'll be", next_year)Working with Decimal Numbers
What happens if you want to ask a user for a number with a decimal point, like their height in metres or the price of a movie ticket?
If you use int() and the user types a decimal like '1.5', your program will crash because int() only understands whole numbers. To fix this, use float() instead. This converts the typed text into a decimal number.
price = float(input("Enter the ticket price: $"))
with_tax = price + 2.50
print("Total price with booking fee: $", with_tax)Your turn — run real Python
The Year 2030 Time Machine
Modify this program so it asks the user for their birth year, converts it into an integer, and calculates how old they will turn in the year 2030.
Bug hunt
If you type the number 7, this program outputs '77' instead of '14'. Why is this happening and how do we fix it?
# Let's double the user's lucky number!
lucky_number = input("What is your lucky number? ")
doubled = lucky_number * 2
print("Double your lucky number is:", doubled)Myth-busting corner
MythIf I type numbers on my keyboard, Python automatically knows to treat them as numbers.
TruthPython is blind to what the characters mean. To Python, everything typed into input() is just a plain sequence of text characters (a string) unless you use int() or float() to explicitly convert it.
MythYou must use int() or float() on every single input() command you write.
TruthYou only need type conversion if you plan to do maths with the value. If you are just asking for a name, a school house, or a favourite food, leave it as a standard text string.
Exam answer that scores full marks
Explain why this code causes an error, and how to fix it: age = input("Enter your age: ") print(age + 5)
This code causes an error because the input() function always returns the user's response as a string (text), even if they type numbers. Python cannot perform mathematical addition between incompatible data types, such as a string and an integer (adding the text '12' to the number 5). To fix this, you must convert the user's input into an integer using the int() function. The corrected line of code should be: age = int(input('Enter your age: ')). This converts the text into a real integer, allowing Python to perform the mathematical addition successfully.
Why it scores
- Correctly identifies that input() always returns text (a string).
- Explains that you cannot mathematically add a string and an integer together.
- Identifies the int() function as the correct tool for type conversion.
- Provides the exact corrected line of Python code.