Exercise 05-15:

Write a program to control a small cash register. You must ask the user to enter the product code and the purchase quantity. Use the following code table to obtain the price of each product:

Code Price
1    0.50
2    1.00 
3    4.00
5    7.00
9    8.00

Your program should display the total of the purchases after the user enters 0. Any other code should generate the “Invalid Code” error message.

Answer:

##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 05/exercise-05-15.py.py
##############################################################################
to_pay = 0
while True:
    code = int(input("Product code (0 to exit): "))
    price = 0
    if code == 0:
        break
    elif code == 1:
        price = 0.50
    elif code == 2:
        price = 1.00
    elif code == 3:
        price = 4.00
    elif code == 5:
        price = 7.00
    elif code == 9:
        price = 8.00
    else:
        print("Invalid code!")
    if price != 0:
        quantity = int(input("Quantity: "))
        to_pay = to_pay + (price * quantity)
print(f"Total to pay R${to_pay:8.2f}")
Click here to download the file