Reliability of a solution

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Reliability of a solution

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement best describes a reliable solution?
A It behaves correctly and consistently every time it is used
B It works correctly only the first time it is run
2. A user types 'abc' where a number is expected. Which response shows reliability?
A Show an error message and ask for the number again
B Let the program crash with a ValueError
3. A program opens 'scores.csv' but the file is missing. Which code is more reliable?
A try: open('scores.csv') with except FileNotFoundError
B f = open('scores.csv') with no error handling
4. Which is an example of consistent results?
A The same input sometimes gives different outputs
B The same input always gives the same output
5. True or false: A solution that crashes when a file is missing is still reliable if it works when the file exists.
A True
B False

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1try: f = open("scores.txt") except FileNotFoundError: print("File missing") ATyping text such as 'abc' causes a ValueError and the program crashes. The input needs validation or try/except so invalid input is handled.
2assert calc_total(2, 3) == 5 Bwhile not age.isdigit(): age = input("Enter age: ")
3A reliable solution deals with invalid input by asking again rather than crashing Cdef double(n): return n * 2
4A reliable solution gives consistent results: the same input always produces the same output DNo error handling: if data.txt is missing the program crashes with FileNotFoundError, so it is not reliable. Wrap it in try/except.
5f = open("data.txt") lines = f.read() EHandles a missing file with an error message instead of crashing, which makes the solution more reliable
6age = int(input("Age: ")) FA test that checks the function gives the correct result, helping confirm the solution is reliable

Definitions

reliability

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
try: f = open("scores.txt") except _________________:
2.
_____ not age.isdigit(): age = input("Age: ")
3.
______ average([2, 4, 6]) == 4
4.
try: n = int(input("Number: ")) except __________:
5.
if os.path.______("data.csv"): f = open("data.csv")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A try:
B f = open('data.txt')
C except FileNotFoundError
D print('File missing')
Question 2
A catch:
B n = int(input('Age: '))
C except ValueError:
D print('Enter a number')
Question 3
A age = -1
B while age < 0 and age > 120:
C age = int(input('Age: '))
Question 4
A def double(n):
B return n + 2
C assert double(3) == 6
D assert double(0) == 0
Question 5
A def average(nums):
B if len(nums) == 0:
C return 1 / 0
D return sum(nums)/len(nums)

Answer Key

Reliability of a solution
Section A: Quick Questions
Q1
A — It behaves correctly and consistently every time it is used
Q2
A — Show an error message and ask for the number again
Q3
A — try: open('scores.csv') with except FileNotFoundError
Q4
B — The same input always gives the same output
Q5
B — False
Section B: Match the Pairs
Q1
try: f = open("scores.txt") except FileNotFoundError: print("File missing") → Handles a missing file with an error message instead of crashing, which makes the solution more reliable
Q2
assert calc_total(2, 3) == 5 → A test that checks the function gives the correct result, helping confirm the solution is reliable
Q3
A reliable solution deals with invalid input by asking again rather than crashing → while not age.isdigit(): age = input("Enter age: ")
Q4
A reliable solution gives consistent results: the same input always produces the same output → def double(n): return n * 2
Q5
f = open("data.txt") lines = f.read() → No error handling: if data.txt is missing the program crashes with FileNotFoundError, so it is not reliable. Wrap it in try/except.
Q6
age = int(input("Age: ")) → Typing text such as 'abc' causes a ValueError and the program crashes. The input needs validation or try/except so invalid input is handled.
Section C: Definitions
Q1
reliability — A solution is reliable when it behaves correctly and consistently every time it is used, even with invalid input or missing files or data. It handles errors without crashing.
Section D: Fill in the Blanks
Q1
try: f = open("scores.txt") except FileNotFoundError:
Q2
while not age.isdigit(): age = input("Age: ")
Q3
assert average([2, 4, 6]) == 4
Q4
try: n = int(input("Number: ")) except ValueError:
Q5
if os.path.exists("data.csv"): f = open("data.csv")
Section E: Spot the Error
Q1
C — An except clause must end with a colon. Without it the program has a syntax error and the missing-file handling never runs. It should be except FileNotFoundError:
Q2
A — Python has no catch keyword. Error handling starts with try:, which lets the program recover from bad input instead of crashing.
Q3
B — No number is both below 0 and above 120, so the loop never runs and invalid ages are never rejected. It should use or.
Q4
B — Adding 2 gives 5 for double(3), so the assert fails. The function must return n * 2 to give correct results every time.
Q5
C — Returning 1 / 0 causes a ZeroDivisionError, so an empty list crashes the program. The guard should return a safe value such as 0.