AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables, input and casting

Week 2 · Unit 7.1: Block it out · 3 lessons
3

Casting to the right type

Computing 7: Unit 7.1, pp. 8–43
GoalExplain why input needs casting before a calculation, and cast with int(), float() and str().
New words
casting · changing a value to another data typeint() · casts to an integerfloat() · casts to a real numberstr() · casts to a string
Explanation

Input always gives back a string. A string that looks like a number is still only text. If you use + on two strings, Python joins them, so "12" + "15" makes "1215". To calculate, you must cast the text to a number first. Wrap the input command in int() for whole numbers, or in float() for real numbers. Use str() to turn a number into text. If the text cannot be a number, such as abc, Python stops with an error. Casting is important because Python does not guess the type.

Python
1mon = input("Pages read on Monday? ")
2tue = input("Pages read on Tuesday? ")
3total = mon + tue
4print("Total pages:", total)
Output
Total pages: 1215
Worked examples
Python
1mon = int(input("Pages read on Monday? "))
2tue = int(input("Pages read on Tuesday? "))
3total = mon + tue
4print("Total pages:", total)
Output
Total pages: 27
Here is the same program with both inputs cast to integers. The user types 12 and 15 again. Now + adds the numbers.
Python
1km = float(input("Distance in km? "))
2metres = km * 1000
3print("Metres:", metres)
Output
Metres: 2500.0
When you cast to float, the answer is a real number. Python shows 2500.0, with a decimal point. The user types 2.5.
CastResultType
int("12")12whole number
float("2.5")2.5real number
float("3")3.0real number
str(7)'7'text
int("abc")ValueError: invalid literal for int() with base 10: 'abc'error
Casting a value of the wrong kind stops the program. Look at each result.
Class activity

Cast cards (no computer): write the digits 5 and 3 on four small cards, two in quotation marks ("5" and "3") and two without (5 and 3). Rule 1: the plus sign on two quoted cards means ‘stick the cards side by side’. Show 53. Rule 2: on two plain cards it means ‘add’. Show 8. Rule 3: a quoted card cannot be added to a plain card: say ‘error’. Make up three more pairs for your partner to solve. Then write the cast you would need to change a quoted card into a number. If you have a computer or tablet, test the three rules in IDLE with an adult.

Practice
1
Python shows this message when the user types 10 and then 25. Explain why and fix the program.
Python
1start = input("Start page? ")
2end = input("End page? ")
3print(end - start)
Output
TypeError: unsupported operand type(s) for -: 'str' and 'str'
2
The user types 14 and then 9. What does the program show? What would it show if both int() casts were missing?
Python
1a = int(input("First number? "))
2b = int(input("Second number? "))
3print(a + b)
3
Choose int or float for each value: (a) the number of players in a team, (b) a price of 3.75, (c) a height of 1.62 metres, (d) the number of goals in a match.
4
The user types 4. The program runs, but it shows 444 instead of 12. Explain what went wrong and write the corrected line 1.
Python
1packs = input("Packs of stickers? ")
2total = packs * 3
3print(total)
Output
444