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

Stop the crash

Week 13 · 1.8 Testing · 3 lessons
1

Why programs crash

Computing 9: Topic 1.8, pp. 135–154
GoalSay what a run-time error is, name three common causes, and spot the input that makes a program crash.
New words
run-time error · a mistake that only shows while the program is running, when it is asked to do something impossiblecrash · the sudden stop of a program, with an error messagelist index · the position of an item in a list; the first item is at 0
Explanation

A program can have correct syntax and sensible logic and still stop half-way through. This is a run-time error, and the stop is called a crash. The cause is usually a request that cannot be carried out. Three common requests are dividing by 0, turning a word such as "ten" into a number with int(), and asking for a list position that does not exist. Python then prints the type of the error and the line where it happened. The same program may run well with one input and crash with another, so a few good test runs do not prove that it is safe. A syntax error stops a program before it starts, a logic error lets it finish with a wrong answer, and a run-time error stops it in the middle.

Kind of errorWhen you notice itExample
Syntax errorbefore the program startsa missing colon after if
Logic errorthe program ends, but the answer is wrong+ typed instead of ×
Run-time errorthe program stops while it is runningdividing by 0
Worked examples
Python
1km = int(input("Kilometres run? "))
2minutes = int(input("Minutes taken? "))
3pace = minutes / km
4print("Pace:", pace, "minutes per km")
Output
ZeroDivisionError: division by zero
Dividing by zero. A runner’s pace is the minutes divided by the kilometres. With 5 km and 30 minutes the program prints Pace: 6.0 minutes per km. The user types 0 for the kilometres, then 30 for the minutes. Nothing can be divided by 0, so the program stops at line 3.
Python
1pages = int(input("Pages read? "))
2print("You have", 100 - pages, "pages left")
Output
ValueError: invalid literal for int() with base 10: 'ten'
Text that is not a number. The user types the word ten instead of 10 for the pages. Python cannot turn ten into a number, so int() fails on line 1 and the program stops before it prints anything. With 40 typed, it prints You have 60 pages left.
Python
1stops = ["Park", "Market", "Library"]
2n = int(input("Stop number? "))
3print("Next stop:", stops[n])
Output
IndexError: list index out of range
A list position that does not exist. The list has three stops, at positions 0, 1 and 2. The user types 3, and stops[3] asks for a fourth item that is not there. With 2 typed the program prints Next stop: Library.
Class activity

Crash or run? (no computer). Write each of these lines on its own card: print(8 / 0), print(int("7") + 1), print([1, 2, 3][3]), print("5" + 2), print("Hi". Add two lines of your own. Sort the cards into three piles: runs and prints an answer, stops while running (run-time error), will not start (syntax error). Check your piles with a partner and argue about any card where you disagree. (If you have a computer or tablet, type the lines into Python to check.)

Practice
1
Two numbers are typed: first 10 and 4, then 10 and 0. What does the program print each time?
Python
1slices = int(input("Slices? "))
2people = int(input("People? "))
3print("Each person gets", slices // people)
2
Say whether each is a syntax error, a logic error or a run-time error. (a) print(10 / 0) (b) a colon is missing after for (c) a program that should find the area of a rectangle uses + instead of × (d) int("seven") (e) colours[5] when the list colours has four items.
3
What does this program print before it stops, which error stops it, and how do you repair the loop?
Python
1prices = [4, 7, 9]
2for i in range(4):
3    print(prices[i])
4
A program reads a number and prints 100 divided by it. A learner tests it with 5, 20 and 50 and all three tests work. Why did the tests not find the crash, and what test would?