Course 2.1Lesson 2 of 4
input() always gives text, even when the user types digits, so adding two answers joins them: 3 and 4 give 34. To calculate, change the text into a number: int() for a whole number and float() for a number with a decimal point. str() changes a number into text so that it can be joined to other text. If the text cannot be a number, for example a word, or 3.5 for int(), Python stops with a ValueError.
Try the code and the simulations beside the lesson.
After this lesson you can
input() gives text, and what goes wrong if you forget it,ValueError and say what the user typed to cause it.Here is the most common mistake in every first program that asks questions.
input() always gives text. It does not matter what the user types. The digits 15 come as the text "15". This is why a program that adds two typed numbers shows 34 for 3 and 4.
To calculate, we must change the text into a number. For a whole number, use int().
Type 15 in the Input box and run it. The best way is to do both steps in one line: int(input("...")). The input is read first, as text, and then int() turns it into a number. Now age + 1 is a real sum, 16, and the type is <class 'int'>.
The same + sign does two different jobs. Before you calculate with an answer from input(), turn it into a number.
For a number with a decimal point, use float().
Type 1.6 in the Input box and run it. float() can read 1.6. It can also read a whole number such as 7, and it gives 7.0. But int() cannot read a decimal point, as we see next.
Sometimes you must go the other way. You can join a number to text only when the number is changed to text first.
What if the user types something that is not a number?
Type 3.5 in the Input box and run it. Python stops with ValueError: invalid literal for int() with base 10: '3.5'. In plain words: the text 3.5 cannot be made into a whole number. Run it again, with the word ten. Same error. A ValueError means "this value is the right kind of thing, but a wrong one".
Later, in the course on try and except, you will learn how to catch this error, so that your program does not stop. For now, be careful with what you type, and use float() when a decimal point is possible.
| You have | You want | Write | Example |
|---|---|---|---|
text, such as "15" | a whole number | int(text) | int("15") is 15 |
text, such as "1.6" | a decimal number | float(text) | float("1.6") is 1.6 |
| a number | text | str(number) | str(15) is "15" |
| a decimal number | a whole number (cut off) | int(number) | int(3.9) is 3 |
int() or float(). The program shows 1515 instead of 30.int() on a number with a decimal point. It gives a ValueError. Use float().+. "Age: " + 15 is a TypeError. Use str(15), or a comma.int(input("Age? ") + 1) is wrong, because the brackets close in the wrong place. Write int(input("Age? ")) + 1.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
input() always gives text.int() makes a whole number, float() a decimal number, str() text.int(input("...")) asks and converts in one line.int(), gives a ValueError.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
4 and 5. What does input() + input() give?2.75?int("hello")?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.