← Teacher guide

Challenge: a console app

Students read a working café till, predict a change, then build a console app with a menu, a checked number and a total.

Console bridge: text in, text out · Console challenge · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 8 min · Modify 8 min · Make 20 min

Running the lesson

  1. Predict: students read the program and work out the total by hand before running.
  2. Run: run with a word as the quantity. Ask which line protected the program.
  3. Modify: encourage one change and a run at a time.
  4. Make: this is the main task. Milestones: it runs and shows a menu; a choice adds to a total; a typed number is checked; it quits with break; three test cases pass.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Planning before typing. Ask for the choices and what ends the program.

When students get stuck

What you will seeA question to askThe fix
The menu never ends.Which line runs break?One branch needs a break, indented inside the loop.
A crash when a word is typed for a quantity.Is int called before or after the isdigit check?Check inside an if first, and convert inside it.

Answers to the checks

  1. Predict: You choose 1, then 2 with a quantity of 3, then 3. What total does the last line show?
    19. One coffee is $4. Choice 2 asks how many and adds $5 for each, so 3 cakes add $15, and 4 + 15 = 19.
  2. Which part does the job?: Which part stops the program crashing when the quantity is a word?
    The isdigit check before int
    text.isdigit() asks whether the quantity is all digits before int is used.
  3. Follow a change: You choose 2 and type 2 as the quantity, and then choose 3. What is the total?
    10
    Two cakes at $5 each are $10. The total starts at 0 and cake adds price × quantity.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

total = 0
while True:
    choice = input("1 coffee $4, 2 cake $5, 3 sandwich $7, 4 pay: ")
    if choice == "1":
        total += 4
        print("Coffee added. Total: $" + str(total))
    elif choice == "2":
        how_many = input("How many cakes? ")
        if how_many.isdigit():
            total += 5 * int(how_many)
            print("Cakes added. Total: $" + str(total))
        else:
            print("Please type a number.")
    elif choice == "3":
        how_many = input("How many sandwiches? ")
        if how_many.isdigit():
            total += 7 * int(how_many)
            print("Sandwiches added. Total: $" + str(total))
        else:
            print("Please type a number.")
    elif choice == "4":
        break
    else:
        print("Please choose 1, 2, 3 or 4.")
print("Please pay $" + str(total))

Make

total = 0
while True:
    choice = input("1 pencil $2, 2 ruler $3, 3 pay: ")
    if choice == "1":
        amount = input("How many pencils? ")
        if amount.isdigit():
            total += 2 * int(amount)
            print("Total: $" + str(total))
        else:
            print("Please type a number.")
    elif choice == "2":
        total += 3
        print("Total: $" + str(total))
    elif choice == "3":
        break
    else:
        print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))

Stretch and support

Stretch: Save the order to a list and print every item at the end.

Support: Give the café with one branch missing and ask for it.

Exit ticket

Name three moves your app uses and the line that shows each.

Any three of: input, int with isdigit, while True with break, +=, if/elif/else, a function.

What each step is for

Where it leads

The Python extensions all start here: objects, data detective, deeper console Python, and apps with Gradio.