Dependability of a program

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

Dependability of a program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which program is more dependable?
A One that crashes when a user types letters instead of a number
B One that gives a clear message when a user types letters instead of a number
2. True or false: A program that works with perfect input every time is automatically dependable.
A False
B True
3. A user enters -5 for their age. Which response shows a dependable program?
A Show an error message and ask for the age again
B Accept -5 and carry on using it
4. Which code is better for dependability?
A try: n = int(input()) except ValueError: print('Numbers only')
B n = int(input())
5. What does error handling help a program do?
A Make sure no user ever makes a mistake
B Keep running or fail safely when something goes wrong

Match the Pairs

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

Left columnRight column
1try: age = int(input()) except ValueError: print('Enter a number') AKeeps asking until the input is valid, so bad data never gets used
2while age < 0 or age > 120: age = int(input('Age: ')) BTyping letters such as 'abc' causes a ValueError and crashes the program, because there is no error handling.
3Reliability: the program gives the right result every time it is run with valid input Cdef add(a, b): return a + b
4Handling invalid input: check the input before using it DCatches bad input so the program shows a message instead of crashing
5age = int(input('Age: ')) print(age + 1) Eif not name.isdigit(): print('Numbers only')
6try: n = int(input()) except: pass FThe error is silently ignored, so n is never set and the program fails later without telling the user what went wrong.

Definitions

dependability

Fill in the Blanks

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

1.
___: n = int(input("Number: ")) except ValueError:
2.
try: n = int(input("Number: ")) except __________:
3.
age = int(input("Age: ")) while age _ 0: age = int(input("Age: "))
4.
if count __ 0: print(total / count)
5.
except ValueError: _____("Please enter a number")

Spot the Error

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

Question 1
A try:
B age = int(input('Age: '))
C catch ValueError:
D print('Enter a number')
Question 2
A while True:
B n = int(input('Number: '))
C if n.isdigit():
D break
E print(int(n))
Question 3
A def safe_div(a, b):
B if b == 0:
C return a / b
D return a / b
Question 4
A try
B f = open('data.txt')
C except FileNotFoundError:
D print('File missing')
Question 5
A score = input('Score: ')
B if score.isdigit():
C print('Invalid score')
D else:
E print('Saved')

Answer Key

Dependability of a program
Section A: Quick Questions
Q1
B — One that gives a clear message when a user types letters instead of a number
Q2
A — False
Q3
A — Show an error message and ask for the age again
Q4
A — try: n = int(input()) except ValueError: print('Numbers only')
Q5
B — Keep running or fail safely when something goes wrong
Section B: Match the Pairs
Q1
try: age = int(input()) except ValueError: print('Enter a number') → Catches bad input so the program shows a message instead of crashing
Q2
while age < 0 or age > 120: age = int(input('Age: ')) → Keeps asking until the input is valid, so bad data never gets used
Q3
Reliability: the program gives the right result every time it is run with valid input → def add(a, b): return a + b
Q4
Handling invalid input: check the input before using it → if not name.isdigit(): print('Numbers only')
Q5
age = int(input('Age: ')) print(age + 1) → Typing letters such as 'abc' causes a ValueError and crashes the program, because there is no error handling.
Q6
try: n = int(input()) except: pass → The error is silently ignored, so n is never set and the program fails later without telling the user what went wrong.
Section C: Definitions
Q1
dependability — A dependable program works correctly and reliably every time, even when users enter unexpected or invalid input, because it handles errors instead of crashing.
Section D: Fill in the Blanks
Q1
try: n = int(input("Number: ")) except ValueError:
Q2
try: n = int(input("Number: ")) except ValueError:
Q3
age = int(input("Age: ")) while age < 0: age = int(input("Age: "))
Q4
if count != 0: print(total / count)
Q5
except ValueError: print("Please enter a number")
Section E: Spot the Error
Q1
C — Python does not use catch. The error handler must be written as 'except ValueError:' so invalid input is handled instead of crashing.
Q2
B — Converting with int() before checking crashes the program if the user types letters. Keep the input as text, check it with isdigit(), and convert afterwards.
Q3
C — Inside the zero check the function still divides by b, so it crashes with ZeroDivisionError. It should return a safe value such as None.
Q4
A — A try block must end with a colon. It should be 'try:' so the error handling code is valid.
Q5
B — The condition is inverted. Valid digits would be reported as invalid. It should be 'if not score.isdigit():' so only bad input is rejected.