Python Forum

Full Version: Get multiple values from function and total it.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have created a python program that is an ordering system in a resturant. The resturant has scheme of if any customer buys 4 pizzas, gets 1 drink for free and if any customer buys 4 pastas, they get 1 free bruchetta. I created the program and it can calculate the total for each order and display it but where I am stuck is, now at the end of the program, I need to display how may orders were taken, how much is the total sales and how many free drink and bruchetta were given.

#Pasta Prices

pasta1=9.5
pasta2=17.00
pasta3=27.50

#Pizza Prices

pizza1=10.99
pizza2=20.99
pizza3=29.99


    
def P():
    pizza=int(input("enter the number of pizzas for this order"))
    if pizza==1:
        pizza_sales=pizza1
    elif pizza==2:
        pizza_sales=pizza2
    elif pizza==3:
        pizza_sales=pizza3
    elif pizza>3:
        quotient=pizza//3
        remainder=pizza%3
        if remainder==1:
            pizza_sales=((quotient*pizza3)+pizza1)
        else:
            pizza_sales=((quotient*pizza3)+pizza2)
    this_order=pizza
    return pizza_sales,this_order,remainder,quotient


        
def PP():

    pasta=int(input("enther the number of pastas for this order"))
    
    if pasta==1:
        pasta_sales=pasta1
    elif pasta==2:
        pasta_sales=pasta2
    elif pasta==3:
        pasta_sales=pasta3
    elif pasta>3:
        quotient1=pasta//3
        remainder1=pasta%3
        if remainder1==1:
            pasta_sales=((quotient1*pasta3)+pasta1)
        else:
            pasta_sales=((quotient1*pasta3)+pasta2)
        this_order1=pasta
    return pasta_sales,this_order1,quotient1,remainder1
        

    


def B():
        pizza_sales,this_order,remainder,quotient=P()
        print("the total for",this_order,"pizzas is",pizza_sales)
        pasta_sales,this_order1,quotient1,remainder1=PP()
        print("the total for",this_order1,"pastas is",pasta_sales)
        print("So the total is",(pizza_sales+pasta_sales))
           


order='m'

while order=='m' or order=='M':    
    print("Please enter the order")
    \
    \
    a=input("Please enter P to order Pizza, T to order pasta or B to order both")
    if a== 'P'or a=='p':
         print("Current promotion: FREE drink for every 4 pizzas bought")
         pizza_sales,this_order,remainder,quotient=P()
         print ("your total for",this_order,"pizza(s) is",pizza_sales)
         print ("Free drinks given:", quotient-1)
         choice=input("Do you want to view the order summary for your last order?")
         if choice=='Y' or choice=='y':
            print ("Number of pizzas bought=",this_order)
            print ("Grand total for the order=",pizza_sales)
            print ("Free drinks given:", quotient)

    elif a=='T'or a== 't':
            pasta_sales,this_order1,quotient1,remainder1=PP()
            print ("pasta sales for this order",pasta_sales,"and the number of pastas is",this_order1)
            print ("Customer gets", quotient1-1 ,"free bruchetta")
            choice=input("Do you want to view the order summary for your last order?")
            if choice=='Y' or choice=='y':
                print ("Number of pastas bought=",this_order1)
                print ("Grand total for the order=",pasta_sales)
                print ("Customer gets", quotient1-1 ,"free bruchetta")
           
     
    
    elif a=='B' or a=='b':
        B()
            
            
        
    else:
        print("invalid order code")
        

    order=input("Press M to take more order and press any other key to end your shift")
I really appreciate everyone's time and effort and your help. If my question is unclear, please let me know so that I can paste the full case scenario to give a full perspective. Thank you
Some observations:

- don't use cryptic names for you functions (P(), PP(), B()). If I understand correctly they could be something like pizza_order, pasta_order and total_order

- is pricing really so that it repeats in blocks of three? Meaning that for fourth pizza I have to pay full price without any discount? You will loose one potential (volume) customer as I will not visit this place with my family (I am father of four). I don't think that paying additional $0.99 for fourth pizza qualifies as 'free drink' Naughty

- there is no need to rename (in function P() pizza --> this_order, in function PP() pasta --> this_order1). You can just give it descriptive name right away. Like 'quantity' or 'order_quantity' or something else.
In P(), if someone orders 6 pizzas, that person gets charged for 8 because the remainder of 6 // 3 is not 1; PP() has the same issue.

To tack onto perfringo's critique, the functions are poorly done. They should have parameters instead of input inside of them. If P() is code like this:

pizza_prices = {
    0: 0.00,
    1: 10.99,
    2: 20.99,
    3: 29.99
}

def P(number, prices):
    price = prices.get(number, prices[len(prices) - 1])
    quotient = number // 3
    remainder = number % 3
    pizza_sales = (quotient * price) + prices[remainder]

    return pizza_sales, number, remainder, quotient

P(10, pizza_prices)
then it fixes the problem from before and is easy to update. You can provide any dict in the format of pizza_prices and change all the pricing. Plus, you can use P() for the pasta as well instead of having two separate functions since they are identical. Now, it still performs too much. If a function returns more than one value, it likely does too much.
I really appreciate your time and help. I am going to code in a new format using your suggestions. My last question is,Let's say I am a customer service guy at Meccas and I take orders for burger and cold drinks during my shift. So, how can I get the total sales and the number of items sold after my shift(which might have no finite number of orders.) Thank you once again guys.
A class would be your best option. The class - let's call it Worker - would need a couple attributes to store the number of sales and the cost. With a method named Worker.take_order(), you could increment those values based on the order that's been placed.
(Feb-05-2019, 01:52 AM)stullis Wrote: [ -> ]A class would be your best option. The class - let's call it Worker - would need a couple attributes to store the number of sales and the cost. With a method named Worker.take_order(), you could increment those values based on the order that's been placed.

I know I should be doing my own research on what you have said but do you mind elaborating on its use if you were to use the class on the code above.

Thanks