← Console bridge: text in, text out

Challenge: a console app

Read a working café till, change one thing and predict the result, then make a console app of your own with a menu, a checked number, a total and tests.

Every app you use is a menu, a check and a total, wearing a nicer coat. Today you build one.

This is the end of the console course. The program below uses everything: input, a checked number, while True, break, a running total and a function. Read it first, then make an app of your own.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
total = 0
while True:
    choice = input("1 coffee $4, 2 cake $5, 3 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":
        break
    else:
        print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))

Read the program.

You choose 1, then 2 with a quantity of 3, then 3. What total does the last line show?

RunRun it and compare the result with your prediction.

Run

Run it and compare with your prediction. Try a quantity that is a word.

lesson.py
total = 0
while True:
    choice = input("1 coffee $4, 2 cake $5, 3 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":
        break
    else:
        print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
total = 0
while True:
    choice = input("1 coffee $4, 2 cake $5, 3 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":
        break
    else:
        print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))

Name the part that protects the program.

Work out a total by hand.

Which part stops the program crashing when the quantity is a word?

You choose 2 and type 2 as the quantity, and then choose 3. What is the total?

ModifyChange the code on purpose and check the result.

Modify

Change one thing at a time, predicting each result before you run it. Add a new item to the menu with its own price, and make pay choice 4.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Copy the cake branch and change the name and the price.

Another hint

Add a line to the menu text so people know the new choice, and move pay to choice 4.

Add a new item to the menu with its own price and a checked quantity.

    Run your program. This ticks when your code does all of the above.

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    Make a console app of your own. Pick a theme: a game shop, a canteen order, a fitness log or a unit converter. It needs:

    Then write three test cases for the part that works out replies, including one wrong input. Change one thing and predict what will happen before you run it.

    You have finished the console course. Every Python extension starts from here.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Plan it on paper first: what are the choices, what is stored, when does it end?

    Another hint

    Start from the café and change the theme, the prices and the replies.

    A console app with a menu loop, a checked number, a running total and a way to quit.

      Run your program. This ticks when your code does all of the above.

      For teachers

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

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      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.

      The hard part (on purpose)

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

      When students get stuck

      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.

      Where it leads

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

      What each step is for

      Now you can
      You reached the endBack to Console bridge: text in, text out