← Intro to programming with Python
Turning text into numbers
Use int() and float() on what people type.
Because input() always gives text, "3" + "4" gives "34", not 7. Python is not being silly: joining two strings is exactly what + does with text. To do math, convert first. int(text) turns "12" into the whole number 12, and float(text) turns "4.75" into the decimal 4.75. Convert right where you read the input, and the rest of your program can treat it as a number.
age = int(input("How old are you? "))
print("Next year you will be", age + 1)
price = float(input("Price? "))
print("With tax:", round(price * 1.08, 2))int(input(...)) reads the answer and converts it in one line. Use float when the answer might have a decimal point.
Common mistake: forgetting to convert, then adding. If someone types 5, input() gives "5" and "5" + 1 is a TypeError.
int("4.5") is an error, because "4.5" is not written like a whole number. Use float() for decimals.
If the person types letters, int() gives a ValueError. You will learn to handle that politely in the errors unit.