Python for beginners · Lesson 5 of 15
Getting input from the user
Ask the user a question with input(), understand why the answer is always text, and turn it into a number with int() or float().
Programs become interactive when they ask questions. input() stops the program, waits for the user to type something and press Enter, and gives back what they typed.
On this site, the code boxes come with the answers already typed in for you (look for the input lines next to the code), so every run prints the same result. Try changing them.
print("What is your name?")
name = input()
print("Hello,", name)What is your name? Hello, Ana
Typed answer: Ana
A prompt inside input()
You can put the question straight into input(). It is shown on the same line, and the cursor waits right after it. In the output box below, the typed answer itself is not shown, so the prompt and the next printed line run together.
city = input("City: ")
print("You typed:", city)City: You typed: Tokyo
Typed answer: Tokyo
input() always gives you text
Whatever the user types, input() returns a string, even if it looks like a number. Adding two strings glues them together instead of adding them. Convert the answer with int() for whole numbers or float() for decimals.
a = input()
b = input()
print(a + b)
print(int(a) + int(b))53 8
Typed answers: 5 and 3
You can convert in the same line as you ask: int(input()). Python runs the inner call first and then converts what it returned.
print("Price of one coffee?")
price = float(input())
print("How many?")
count = int(input())
total = price * count
print(f"Total: {total:.2f}")Price of one coffee? How many? Total: 7.20
Typed answers: 2.40 and 3
int("3.5") or int("ten") stops the program with a ValueError, because the text is not a whole number. For now, type sensible answers; in Errors and exceptions you will learn to catch these mistakes.print("What year were you born?")
year = int(input())
age = 2026 - year
print(f"In 2026 you turn {age}.")What year were you born? In 2026 you turn 36.
Typed answer: 1990
✎ Exercise
Ask for a temperature in Celsius, convert it to Fahrenheit (multiply by 9, divide by 5, add 32) and print both in one sentence like 20.0 Celsius is 68.0 Fahrenheit. Read the answer with float(input()); the typed answer is 20.
Hint: Use float(input()) so decimals like 21.5 also work.
print("Temperature in Celsius?")
# your code here
Expected output:
Temperature in Celsius? 20.0 Celsius is 68.0 Fahrenheit
Show a solution
print("Temperature in Celsius?")
celsius = float(input())
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius} Celsius is {fahrenheit} Fahrenheit")Temperature in Celsius? 20.0 Celsius is 68.0 Fahrenheit
Check yourself
1. What type does input() return?
input() always returns a string. You convert it yourself when you need a number.
2. The user types 4 twice. What does print(input() + input()) show?
Both answers are strings, and + joins strings: "4" + "4" is "44".
3. Which line reads a decimal number like 2.75 correctly?
input() reads the text, then float() turns it into a decimal number. int() would fail on 2.75.