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

Term 1 round-up

Week 11 · Review: Term 1 · 3 lessons
1

Review: Python programs

Computing 7: Units 7.1–7.2, pp. 8–81
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.

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.
ProgramLast line of Python’s messageKind 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 strruntime error: it stops while running
laps = int(input("Laps? ")), and the user types fourValueError: invalid literal for int() with base 10: 'four'runtime error
total = 5, then print(cost)NameError: name 'cost' is not definedruntime 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.
TestTest dataExpected outcomeActual outcomePass/fail
1fare = 12, tip = 4Each pays: 8.0Each pays: 14.0fail
2fare = 10, tip = 0Each pays: 5.0Each pays: 10.0fail
3fare = 0, tip = 4Each pays: 2.0Each pays: 2.0pass
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.
Python
1packs = input("Packs of cards? ")
2doubled = int(packs) * 2
3print(packs + packs)
4print(doubled)
2
The user types 9. Python stops with the message shown. Explain why, and correct the program so that it shows the half.
Python
1width = input("Width in cm? ")
2half = width / 2
3print(half)
Output
TypeError: unsupported operand type(s) for /: 'str' and 'int'
3
Copy the table and write what Python shows for each expression. Say which results are text and which are numbers.
ExpressionWhat Python shows
"7" + "2"?
int("7") + int("2")?
float("7") + 2?
str(7) + "2"?
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.