dcf.cymru stimulus sheet · Computer Science · Year 11

Pwll Glan-y-Dŵr: the swimming lesson booking program

The pool, the client and the program are invented for practice. This is not your class’s Unit 2 pre-release brief. The Python is real and runs exactly as described.

1. The client brief

Pwll Glan-y-Dŵr is a small leisure centre pool. Its manager, Siw Morgan, asked a developer for a program that books children onto blocks of swimming lessons at the reception desk. These are her requirements.

RefRequirement
R1The receptionist types the child’s surname, age and the number of lessons in the block.
R2The age must be a whole number from 4 to 16, including 4 and 16. Anything else is rejected with the message Age must be 4 to 16.
R3The number of lessons must be a whole number from 1 to 10, including 1 and 10. Anything else is rejected with the message Lessons must be 1 to 10.
R4Each lesson costs £6.50. A block of 6 or more lessons gets 10% off the total.
R5The booking reference is the first 3 letters of the surname in capitals, followed by the age. Jones, aged 9, is JON9.
R6The program must never crash, whatever the receptionist types.

2. The program the developer handed over

In the finished system these functions are called when the receptionist clicks a Tkinter Book button. Here input() and print() stand in for the entry boxes and the message label, so you can trace it on paper. The developer says it “works fine”. Siw is not so sure.

1 # Glan-y-Dwr pool: swimming lesson booking (invented practice scenario) 2 PRICE_PER_LESSON = 6.50 3 DISCOUNT_LESSONS = 6 # a block of 6 or more lessons gets 10% off 4 5 def check_age(age_text): 6 age = int(age_text) 7 if age > 4 and age <= 16: 8 return True 9 else: 10 return False 11 12 def check_lessons(lessons): 13 if lessons >= 1 or lessons <= 10: 14 return True 15 else: 16 return False 17 18 def block_price(lessons): 19 total = lessons * PRICE_PER_LESSON 20 if lessons >= DISCOUNT_LESSONS: 21 total = total * 0.1 22 return total 23 24 def make_reference(surname, age_text): 25 return surname[0:3].upper() + age_text 26 27 surname = input("Surname: ") 28 age_text = input("Age: ") 29 if check_age(age_text): 30 lessons = int(input("Number of lessons: ")) 31 if check_lessons(lessons): 32 reference = make_reference(surname, age_text) 33 price = block_price(lessons) 34 print(f"Booked: {reference}, total £{price:.2f}") 35 else: 36 print("Lessons must be 1 to 10") 37 else: 38 print("Age must be 4 to 16")

3. Python facts you may need

CodeWhat happens
int("12")Gives the whole number 12.
int("ten"), int(""), int("9.5")The program crashes with ValueError: invalid literal for int() with base 10. Nothing after that line runs.
"12".isdigit()True: every character is a digit.
"ten".isdigit(), "".isdigit(), "-3".isdigit(), "9.5".isdigit()False every time.
A and BTrue only when both A and B are true.
A or BTrue when at least one of A and B is true.
"Jones"[0:3]"Jon": characters 0, 1 and 2. .upper() then makes it "JON".
f"£{3.9:.2f}"£3.90: always 2 decimal places.

4. Test data words