Simplicity in program design

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

Simplicity in program design

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which approach follows the KISS idea for checking if a number n is even?
A return n % 2 == 0
B if n % 2 == 0: result = True else: result = False, then return result after extra steps
2. True or false: A simple program is one that solves the problem with the clearest logic, even if it looks less 'clever'.
A False
B True
3. Which function follows the 'one job per function' idea?
A calculate_total(prices) only adds up the prices and returns the total
B process_order(prices) asks for input, adds the prices, prints a receipt and saves a file
4. A student's program has 3 nested loops to find the largest number in a list, but one loop with max() works. What does KISS suggest?
A Use the single loop or max() because it is simpler
B Keep the 3 nested loops because it shows more skill
5. Why is simple code easier to test?
A Simple code never needs testing because it cannot contain errors
B There are fewer paths and each part does one thing, so test cases are easier to design

Match the Pairs

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

Left columnRight column
1def is_even(n): return n % 2 == 0 AA simple one-job function: it only checks whether a number is even and returns True or False.
2total = sum(marks) BUses the built-in sum() to add up a list in one clear line instead of writing a longer loop.
3Give each function one job so it is easier to test and reuse Cdef calc_average(marks): return sum(marks) / len(marks)
4Avoid unnecessary complexity by returning the condition result directly DThis is unnecessarily complex. The comparison already gives True or False, so use result = score >= 50.
5def do_everything(): name = input('Name: ') mark = int(input('Mark: ')) print(name, mark * 2) save_to_file(name, mark) Ereturn age >= 18
6if score >= 50: result = True else: result = False FOne function does many jobs: input, calculation, printing and saving. It should be split into small functions with one job each.

Definitions

KISS

Fill in the Blanks

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

1.
___ even(n): return n % 2 == 0
2.
def area(w, h): return w _ h
3.
def double(n): ______ n * 2
4.
adult = age __ 18
5.
for mark __ marks: print(mark)

Spot the Error

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

Question 1
A def is_even(n):
B # returns True or False
C return n % 2 = 0
Question 2
A def average(nums):
B total = sum(nums)
C count = len(nums)
D return total * count
Question 3
A def get_name():
B return input('Name: '
C name = get_name()
D print('Hi', name)
Question 4
A def area(w, h)
B return w * h
C print(area(3, 4))
Question 5
A def show_grade(mark):
B if mark >= 50:
C return 'Pass
D return 'Fail'

Answer Key

Simplicity in program design
Section A: Quick Questions
Q1
A — return n % 2 == 0
Q2
B — True
Q3
A — calculate_total(prices) only adds up the prices and returns the total
Q4
A — Use the single loop or max() because it is simpler
Q5
B — There are fewer paths and each part does one thing, so test cases are easier to design
Section B: Match the Pairs
Q1
def is_even(n): return n % 2 == 0 → A simple one-job function: it only checks whether a number is even and returns True or False.
Q2
total = sum(marks) → Uses the built-in sum() to add up a list in one clear line instead of writing a longer loop.
Q3
Give each function one job so it is easier to test and reuse → def calc_average(marks): return sum(marks) / len(marks)
Q4
Avoid unnecessary complexity by returning the condition result directly → return age >= 18
Q5
def do_everything(): name = input('Name: ') mark = int(input('Mark: ')) print(name, mark * 2) save_to_file(name, mark) → One function does many jobs: input, calculation, printing and saving. It should be split into small functions with one job each.
Q6
if score >= 50: result = True else: result = False → This is unnecessarily complex. The comparison already gives True or False, so use result = score >= 50.
Section C: Definitions
Q1
KISS — Keep It Simple: solve the problem with the clearest, shortest logic, avoiding extra complexity and giving each function one job, so code is easier to read, test and maintain.
Section D: Fill in the Blanks
Q1
def even(n): return n % 2 == 0
Q2
def area(w, h): return w * h
Q3
def double(n): return n * 2
Q4
adult = age >= 18
Q5
for mark in marks: print(mark)
Section E: Spot the Error
Q1
C — A single = assigns a value and is invalid inside a return. Use == to compare. The one-line return keeps this function simple and focused on one job.
Q2
D — An average is the total divided by the count, so it should be total / count. Multiplying gives the wrong answer.
Q3
B — The input( call is missing its closing bracket. It should be return input('Name: '). The function does just one job: get the name.
Q4
A — A function header must end with a colon. It should be def area(w, h): so the indented body belongs to the function.
Q5
C — The string 'Pass' is missing its closing quote. It should be return 'Pass'. Without it Python raises a syntax error.