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 solution is MORE reliable?
A A program that shows a friendly message when a user types letters instead of a number
B A program that crashes when a user types letters instead of a number
2. A student's quiz app gives the same total score every time the same answers are entered. What does this show?
A The solution is not reliable because it never changes
B Repeatable results, so the solution is reliable
3. A program crashes halfway through saving an order. Which outcome is reliable?
A The half-saved order is rolled back and no data is lost or corrupted
B The half-saved order stays in the database in a broken state
4. Which Python code makes a solution more reliable when reading a number?
A try: age = int(input()) except ValueError: print('Enter a number')
B age = int(input())
5. Which statement about reliability is TRUE?
A A reliable solution only needs to work when the user enters perfect data
B A reliable solution gives correct results every time it is run

Match the Pairs

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

Left columnRight column
1try: qty = int(text) except ValueError: qty = 0 ASELECT COUNT(*) FROM students WHERE house = 'Red';
2conn.commit() BSaves changes to the database so the data is not lost after the program ends
3A reliable solution gives repeatable results: the same query on the same data gives the same answer every time CThe change is never committed and errors are not handled, so data can be lost if the program ends or fails part way through.
4A reliable solution keeps asking until the input is valid, so bad input does not break it DHandles invalid input so the program keeps running instead of crashing
5qty = int(input('Qty: ')) EThere is no error handling. If the user types letters, int() raises a ValueError and the program crashes, so it is not reliable.
6cur.execute(sql) # no commit or error check Fwhile not age.isdigit(): age = input('Age: ')

Definitions

reliability

Fill in the Blanks

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

1.
try: age = int(input("Age: ")) except __________:
2.
while not age._______(): age = input("Enter digits: ")
3.
except sqlite3.Error: conn.________()
4.
cur.execute(sql, (name, mark)) conn.______()
5.
assert total(2, 3) __ 5

Spot the Error

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

Question 1
A age = input('Age: ')
B while not age.isdigit():
C print('Again: ')
D age = int(age)
Question 2
A try:
B n = input('Num: ')
C except ValueError:
D print('Not a number')
Question 3
A try:
B cur.execute(sql, data)
C con.commit()
D except ValueError:
E con.rollback()
Question 4
A SELECT name, price
B FROM products
C ORDER BY RANDOM();
Question 5
A while False:
B s = input('Mark: ')
C if s.isdigit():
D break
E print('Valid mark')

Answer Key

Reliability of a solution
Section A: Quick Questions
Q1
A — A program that shows a friendly message when a user types letters instead of a number
Q2
B — Repeatable results, so the solution is reliable
Q3
A — The half-saved order is rolled back and no data is lost or corrupted
Q4
A — try: age = int(input()) except ValueError: print('Enter a number')
Q5
B — A reliable solution gives correct results every time it is run
Section B: Match the Pairs
Q1
try: qty = int(text) except ValueError: qty = 0 → Handles invalid input so the program keeps running instead of crashing
Q2
conn.commit() → Saves changes to the database so the data is not lost after the program ends
Q3
A reliable solution gives repeatable results: the same query on the same data gives the same answer every time → SELECT COUNT(*) FROM students WHERE house = 'Red';
Q4
A reliable solution keeps asking until the input is valid, so bad input does not break it → while not age.isdigit(): age = input('Age: ')
Q5
qty = int(input('Qty: ')) → There is no error handling. If the user types letters, int() raises a ValueError and the program crashes, so it is not reliable.
Q6
cur.execute(sql) # no commit or error check → The change is never committed and errors are not handled, so data can be lost if the program ends or fails part way through.
Section C: Definitions
Q1
reliability — A reliable solution gives correct results every time and keeps working when input is bad or an error occurs, so no data is lost.
Section D: Fill in the Blanks
Q1
try: age = int(input("Age: ")) except ValueError:
Q2
while not age.isdigit(): age = input("Enter digits: ")
Q3
except sqlite3.Error: conn.rollback()
Q4
cur.execute(sql, (name, mark)) conn.commit()
Q5
assert total(2, 3) == 5
Section E: Spot the Error
Q1
C — The loop never asks for new input, so bad input stays in age and the loop never ends. The line should be age = input('Again: ') so the user can fix their entry.
Q2
B — Without int() the text is never converted, so no ValueError is raised and bad input is accepted as if it were a number. It should be n = int(input('Num: ')).
Q3
D — A database failure raises sqlite3.Error, not ValueError. The rollback would never run, so a half-finished change could be left in the database. It should be except sqlite3.Error:.
Q4
C — RANDOM() puts the rows in a different order each run, so the results are not repeatable. Ordering by real columns gives the same output every time.
Q5
A — With while False the loop body never runs, so the input is never asked for or checked. It should be while True: and then break once the input is valid.