Python Forum
Get multiple values from function and total it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get multiple values from function and total it.
#1
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
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __init__() got multiple values for argument 'schema' dawid294 4 1,886 Jan-03-2024, 09:42 AM
Last Post: buran
  Adding values with reduce() function from the list of tuples kinimod 10 2,513 Jan-24-2023, 08:22 AM
Last Post: perfringo
  How to combine multiple column values into 1? cubangt 15 2,630 Aug-11-2022, 08:25 PM
Last Post: cubangt
  function accepts infinite parameters and returns a graph with those values edencthompson 0 812 Jun-10-2022, 03:42 PM
Last Post: edencthompson
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Function - Return multiple values tester_V 10 4,319 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,441 Mar-09-2021, 12:24 PM
Last Post: KMV
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,847 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  function not giving back total SephMon 1 1,644 Aug-25-2020, 12:46 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,418 Aug-10-2020, 02:51 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020