Text to numbers, explained simply
Try adding "2" + "3" in Python and you get "23", not 5. That is because the quotes make them text, and adding text just sticks the pieces together.
int() turns text that looks like a whole number into a real number you can do maths with. float() does the same for numbers with a decimal point, like 2.5.
This matters most with input(), because answers always arrive as text. Wrap the input in int() when you expect a whole number.
Example: Count the lemonade cups
cups = int(input("How many cups? "))
price = 10
print(f"That costs {cups * price} rupees.")Output
How many cups? 3
That costs 30 rupees.Try this
- Remove int() and run it again. What happens to the answer?
- Ask for two numbers and print their total.
- Type a word instead of a number and read the error. Later you will learn how to handle it.
