Paul's Python CoursePPC

Why does input() give me text instead of a number?

, 2 minutes to read

input() always gives back a string, whatever the person types, so an answer of 20 arrives as the text "20" and not the number 20. To do sums with it, turn it into a number first, with int() for whole numbers or float() for numbers that can have a decimal point, as in bill = float(input("Bill? ")).

Why Python does this

input() has no idea what you're going to do with the answer. Someone might type a price, a name, a postcode or 007, and they all come off the keyboard as characters. So Python hands you exactly what was typed, as text, and leaves it to you to decide what it should be.

What makes this slippery is that it often doesn't crash. Text can be joined with +, so this program runs happily and gets the sum wrong.

a = input("First number? ")
b = input("Second number? ")
print(a + b)

Type 4 and 5 and it prints 45, because + between two strings sticks them together. You only get an error when the text meets a real number. If bill came from input(), then print(bill + 5) stops the program with this.

TypeError: can only concatenate str (not "int") to str

In other words, a string can only be joined to another string.

The example below asks for a bill and shows its type before and after converting it. Run it and type 20. The first bill * 2 prints 2020, which is the string written out twice, and after float() the same sum gives 40.0. Then run it again with 20.50, and try changing float to int on line 5 to see which answers int() will accept.

Run it

main.py

When it asks, try typing "20".

bill = input("How much was the bill? ")
print(type(bill))
print(bill * 2)

bill = float(bill)
print(type(bill))
print(bill * 2)

Output

Press Run and what your program prints shows up here.

Converting on the same line

Most programs convert the answer straight away, on the line that asks the question, so the variable never holds the text at all.

bill = float(input("Bill? "))
people = int(input("People? "))

Python works from the inside out here. input() runs first and hands its text to float() or int(), and only the number ends up in the variable. Use int() for things you count, like people, and float() for things like money, where a decimal point is likely.

If someone types 2.5 where int() is waiting, the program stops with ValueError: invalid literal for int() with base 10: '2.5'. That's int() telling you it can't read that text as a whole number. It doesn't round it for you.

A small exercise

Write a program that asks How old are you? and then says how old the person will be next year. If they type 14, it should print Next year you'll be 15.

Show an answer
age = int(input("How old are you? "))
print(f"Next year you'll be {age + 1}.")

If you leave out int(), the program stops at age + 1 with the same TypeError as above, because age is still the text "14".

You use this for real in the Bill Splitter project, where Caitlin needs the class dinner bill split six ways. The Handbook has more on what input() gives back and on int() if you want to go further.

The project that goes with it

Bill Splitter is a free project in Act 1, for Caitlin. It runs in your browser with nothing to install, and you don't need an account for it.

More guides