Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Get multiple values from function and total it. nirlep Unladen Swallow Posts: 3 Threads: 1 Joined: Feb 2019 Reputation: 0 Likes received: 0 #1 Feb-03-2019, 12:48 AM 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 perfringo Da Bishop Posts: 1,351 Threads: 7 Joined: Jun 2018 Reputation: 136 Likes received: 298 #2 Feb-03-2019, 09:17 AM (This post was last modified: Feb-03-2019, 09:17 AM by perfringo. Edited 1 time in total.) 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' - 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. stullis Minister of Silly Walks Posts: 421 Threads: 1 Joined: Sep 2018 Reputation: 59 Likes received: 68 #3 Feb-03-2019, 03:13 PM (This post was last modified: Feb-03-2019, 03:13 PM by stullis. Edited 1 time in total. Edit Reason: Curse you, formatting! A pox on your parents! ) 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. nirlep Unladen Swallow Posts: 3 Threads: 1 Joined: Feb 2019 Reputation: 0 Likes received: 0 #4 Feb-04-2019, 11:36 AM 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. stullis Minister of Silly Walks Posts: 421 Threads: 1 Joined: Sep 2018 Reputation: 59 Likes received: 68 #5 Feb-05-2019, 01:52 AM 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. nirlep Unladen Swallow Posts: 3 Threads: 1 Joined: Feb 2019 Reputation: 0 Likes received: 0 #6 Feb-05-2019, 06:54 AM (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 « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Inserting values from multiple lists sqlite azulu 1 233 May-24-2020, 08:40 AM Last Post: ibreeden UnUnloading values from multiple widgets in a container UGuntupalli 3 233 Apr-20-2020, 08:53 PM Last Post: UGuntupalli Return values for use outside of function willowman 1 130 Apr-13-2020, 07:00 AM Last Post: buran Function with many arguments, with some default values medatib531 3 248 Mar-14-2020, 02:39 AM Last Post: medatib531 Using function *args to multiply multiple arguments allusernametaken 8 477 Nov-20-2019, 12:01 AM Last Post: allusernametaken auto supply values to a python script(function) from text file metro17 4 385 Oct-26-2019, 01:25 AM Last Post: ichabod801 Save all values to pandas of multiple classes jenniferruurs 0 249 Sep-13-2019, 12:10 PM Last Post: jenniferruurs Custom Function to Return Database Values rm78 0 274 Sep-05-2019, 01:01 PM Last Post: rm78 Passing Values of Dictionaries to Function & Unable to Access Them firebird 3 449 Aug-03-2019, 10:25 PM Last Post: firebird Help trying to replace values in a function Rochense 1 432 Apr-01-2019, 09:59 PM Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)