Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 2 · Lab

Lab: trace a decision, repair a faulty if and test a program

Trace a three-way decision, repair the commonest if error and write a decision program with a test table.

  • 50 minutes
  • 15 marks
  • Self-marked

50 minutes. Self-marked against the rubric. 15 marks. In the full course this counts toward the labs and cases share (15% of the course grade). Write your answers in the boxes, then open the model answer and tick each mark you earned honestly. Nothing is sent or stored.

Scenario (invented)

A park (invented) sets its entry fee by age. Here is Program A, used to show the weather sign:

temp = 18
if temp >= 25:
    print("Warm")
elif temp >= 15:
    print("Mild")
else:
    print("Cold")
print("Done")

And Program B, which a trainee wrote for the fee:

age = int(input("Age: "))
if age = 18:
    print("Adult fee")

Tasks

Task 1 (3 marks)

State the exact output of Program A.

Private: nothing you type here is sent or stored.

Task 2 (3 marks)

Program B fails before it runs. Name the problem and the line.

Private: nothing you type here is sent or stored.

Task 3 (5 marks)

Write a program that reads age and prints Free for under 5, Child for 5 to 17 and Adult for 18 or more.

Private: nothing you type here is sent or stored.

Task 4 (4 marks)

List four test ages with the expected output for each, including the boundaries.

Private: nothing you type here is sent or stored.

Rubric and model answer

Task 1: trace program A (3 marks)

Model: two lines. Mild (18 is not >= 25, but is >= 15), then Done.

Task 2: find the fault (3 marks)

Model: line 2 uses = (assignment) where a comparison == is needed, so Python reports a syntax error.

Task 3: write the fee program (5 marks)

Model: age = int(input("Age: ")) then if age < 5: print Free, elif age < 18: print Child, else: print Adult.

Task 4: test it (4 marks)

Model: 4 gives Free; 5 gives Child; 17 gives Child; 18 gives Adult.

Marks so far: 0 of 15

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards