GoalRead, predict, trace and fix Python programs that use input, casting, arithmetic and test plans.
New words
casting · changing a value from one data type to another, for example with int() or float()trace table · a table that records the value of each variable after every stepruntime error · a mistake that stops the program while it is runninglogic error · a mistake that lets the program run but gives a wrong answer
Explanation
The function input() always gives text, so a value that is used in a calculation must be cast first: int() for whole numbers and float() for real numbers. Without a cast, + joins two pieces of text, and - or / stop the program with a TypeError. Python follows BIDMAS, so brackets come first, then powers, then division and multiplication, then addition and subtraction. The operator / always gives a real number, so 8 / 2 is 4.0. A syntax error stops a program from starting, a runtime error stops it while it runs, and a logic error gives a wrong answer with no message at all. A test plan finds logic errors: you work out the expected outcome by hand before you run the program, and then you compare it with the actual outcome.
input() gives text→int() or float()→calculation→print() shows the answer
Worked examples
Python
1tickets = int(input("Tickets? "))
2price = float(input("Price of one ticket? "))
3total = tickets * price
4print("Total:", total)
Output
Total: 7.5
A puppet show sells tickets. This program casts both answers, so * multiplies numbers and does not repeat text. The user types 3 and then 2.5. Because the price is a real number, the total is a real number too: 3 × 2.5 = 7.5.
Program
Last line of Python’s message
Kind of error
print("Done)
SyntaxError: unterminated string literal (detected at line 1)
syntax error: the program will not start
print("Score: " + 5)
TypeError: can only concatenate str (not "int") to str
runtime error: it stops while running
laps = int(input("Laps? ")), and the user types four
ValueError: invalid literal for int() with base 10: 'four'
runtime error
total = 5, then print(cost)
NameError: name 'cost' is not defined
runtime error
Four small programs, four messages. Python reports a syntax error before it starts the program. The other three programs start, and then stop at the line that cannot be done. The last line of the message names the kind of problem.
Test
Test data
Expected outcome
Actual outcome
Pass/fail
1
fare = 12, tip = 4
Each pays: 8.0
Each pays: 14.0
fail
2
fare = 10, tip = 0
Each pays: 5.0
Each pays: 10.0
fail
3
fare = 0, tip = 4
Each pays: 2.0
Each pays: 2.0
pass
Two friends share a taxi fare and a tip. The line each = fare + tip / 2 is wrong, because BIDMAS divides only the tip. The correct line is each = (fare + tip) / 2. Test 3 passes by luck, so one pass does not prove that a program is right.
Class activity
Predict and check (no computer): each player writes a four-line program on paper. It must ask one question, cast the answer with int() or float(), do one calculation and print a sentence, for example about the stamps in an album. Swap papers. Your partner predicts the output for two different inputs and writes a small trace table. The writer then checks both predictions by hand. Finally, change one int() to float() and say how the output changes. If you have a computer or tablet, type one program into IDLE and compare the real output with your prediction.
Practice
1
The user types 6. What does the program show? Write each line of output.
packs holds the text 6, so packs + packs joins the text and the first line is 66. The second line is the cast value 6 multiplied by 2, which is 12. The output is 66 and then 12.
2
The user types 9. Python stops with the message shown. Explain why, and correct the program so that it shows the half.
TypeError: unsupported operand type(s) for /: 'str' and 'int'
input() gives text, and text cannot be divided by 2. Cast on line 1: width = int(input("Width in cm? ")). With 9 the program then shows 4.5, because / always gives a real number.
3
Copy the table and write what Python shows for each expression. Say which results are text and which are numbers.
Expression
What Python shows
"7" + "2"
?
int("7") + int("2")
?
float("7") + 2
?
str(7) + "2"
?
The first and the last results (72) are text, because the pieces are joined. The second result is the whole number 9, and the third is the real number 9.0.
4
Write a Python program that asks for the number of rows of chairs and the number of chairs in each row, and then shows Chairs: and the total. Work out by hand what it shows for 4 rows of 15 chairs.
Program: 1 rows = int(input("Rows of chairs? ")) | 2 per_row = int(input("Chairs in each row? ")) | 3 total = rows * per_row | 4 print("Chairs:", total) For 4 and 15 it shows Chairs: 60, because 4 × 15 = 60.