Home > Software engineering >  Figuring out how to format the receipt part of my pizza function (Attached is the current output I h
Figuring out how to format the receipt part of my pizza function (Attached is the current output I h

Time:10-29

This is my current output that I have

Your order:
 [['S', 'ONION', 'BACON', 'HAM', 'MUSHROOM'], ['L', 'BACON', 'CHICKEN', 'SAUSAGE', 'GROUND BEEF']]
7.99
8.49
11.99
Tax: $2.79
Total: $24.27

Here I want to format it so the first list is a receipt for its own pizza so it would be like this:

pizza 1: S                    7.99 (#just the base price of the pizza)
- onion
- bacon
- ham
- mushroom
Extra topping (S)      0.50
pizza 2: L
- bacon
- chicken
- sausage
- ground beef
Tax:                          2.79
Total:                       24.27

I don't know how to make the receipt identify that each list in the order it is will be pizza 1 and what size that pizza will be and how to make the toppings of the respective pizzas appear accordingly. Also, what would be the best way to format the code so I can move the total and tax to the right.

My guess it would have to do something with it being a list of lists but I can't wrap my head around on how to identify each list for an individual pizza in the receipt and how to format the toppings like it's asking for. My code for the receipt looks like this, it's a program that is imported into my order program as a heads up.

def generateReceipt(pizzaOrder):
    cost = 0
    rawTotal=0
    rawTax = 0
    finalCost=0
    totalCost = 0
    if len(pizzaOrder)==0:
        print("You did not order anything")
        exit()
    for pizzas in pizzaOrder:
        if pizzas [0]=="S":
            cost = 7.99
            totalCost =cost
            print(cost)
            if len(pizzas)>4:
                extraTopping = len(pizzas)-4
                toppingExtra= 0.5 * extraTopping
                totalCost =toppingExtra
        if pizzas [0]=="M":
            cost = 9.99
            totalCost =cost
            if len(pizzas)>4:
                extraTopping = len(pizzas)-4
                toppingExtra = 0.75 * extraTopping
                totalCost =toppingExtra
                print(cost)
        if pizzas [0]=="L":
            cost = 11.99
            totalCost =cost
            if len(pizzas)>4:
                extraTopping = len(pizzas)-4
                toppingExtra = 1.00 * extraTopping
                totalCost =toppingExtra
                print(cost)
        if pizzas [0]=="XL":
            cost = 13.99
            totalCost =cost
            if len(pizzas)>4:
                extraTopping = len(pizzas)-4
                toppingExtra = 1.25 * extraTopping
                totalCost =toppingExtra
                print(cost)
    rawTax= totalCost*0.13
    rawTotal=totalCost rawTax
    finalCost = f'${rawTotal:.2f}'
    Tax = f'${rawTax:.2f}'
    print('Tax:',Tax)
    print('Total:',finalCost)
import PizzaReceipt
LIST = ('ONION', 'TOMATO', 'HOT PEPPER', 'GREEN PEPPER', 'MUSHROOM', 'OLIVE', 'SPINACH', 'BROCCOLI', 'PINEAPPLE', 'PEPPERONI', 'HAM', 'BACON', 'GROUND BEEF', 'CHICKEN', 'SAUSAGE')
Yourorder = []
pizzaOrder = []
yourToppings = ()
orderPizza = input("Do you want to order some pizza? ").upper()
orderContinue = "YES".upper()
if orderPizza.upper() == orderPizza.upper() == "Q" or orderPizza.upper() == "NO":
    PizzaReceipt.generateReceipt(pizzaOrder)
    exit()
else:
    while orderContinue.upper() == "YES":
        Yourorder = []
        size = ""
        while size.upper() != "S" and size.upper() != "M" and size.upper() !="L" and size.upper()!="XL":
            size = input("Choose a size: S, M, L, or XL: ").upper()
            Yourorder.append(size)
            yourToppings = ""
        while yourToppings != "X":
            yourToppings = input("Type in one of our toppings to add it to your pizza. To see the list of our toppings, enter\n\"LIST\". When you are done adding toppings, enter \"X\"\n").upper()
            if yourToppings.upper() in LIST:
                print("\nSuccessfully added,",yourToppings)
                Yourorder.append(yourToppings)
            elif yourToppings == "LIST".upper():
                print(LIST)
            elif yourToppings == "X":
                print('Finished ordering pizza')
                print(Yourorder)
            else:
                print("Invalid input")
        orderContinue = input("Do you want to order another pizza? ")
        pizzaOrder.append(Yourorder.copy())
        if orderContinue.upper() == "NO":
            print('Your order: \n',pizzaOrder)
PizzaReceipt.generateReceipt(pizzaOrder)

CodePudding user response:

Does this work for you?

def generateReceipt(order):
    basePrices = {'S': 7.99, 'M': 9.99, 'L': 11.99, 'XL': 13.99}
    extraToppingPrices = {'S': 0.5, 'M': 0.75, 'L': 1.0, 'XL': 1.25}
    total = 0.0
    for (i, pizza) in enumerate(order):
        total  = basePrices[pizza[0]]
        print(f'pizza {i 1}: {pizza[0]}\t\t{basePrices[pizza[0]]}')
        for topping in pizza[1:]:
            print(f'- {topping}')
        for extra in range(len(pizza) - 4):
            total  = extraToppingPrices[pizza[0]]
            print(f'Extra topping: ({pizza[0]})\t{extraToppingPrices[pizza[0]]}')
    print(f'Tax: ${(total * 0.13):.2f}')
    total  = total * 0.13
    print(f'Total: ${total:.2f}')


generateReceipt([['S', 'ONION', 'BACON', 'HAM', 'MUSHROOM'],
                 ['L', 'BACON', 'CHICKEN', 'SAUSAGE', 'GROUND BEEF']])

The key thing here is f-strings. They're really useful and I encourage you to learn more about them.

  • Related