Reputation: 43
NEW EDIT
the result is this when I only enter 1 coffee
Item: Juice Cost: OrderCount: 1
Item: Coffee Cost: OrderCount: 1
Item: Lemonade Cost: OrderCount: 1
Item: Soda Cost: OrderCount: 1
Item: Water Cost: OrderCount: 1
7% TAX:
-------
TOTAL BALANCE:
--------------------------------
This is the relevant code snippet:
if totaldrinks > 0:
print("DRINK")
d = {}
if coffee > 0:
d['Coffee'] = (coffee, drink.count(countcoffee)) # d[menu_item] = (cost, order_count)
if juice > 0:
d['Juice'] = (juice, drink.count(countjuice)) # d[menu_item] = (cost, order_count)
if soda > 0:
d['Soda'] = (soda, drink.count(countsoda)) # d[menu_item] = (cost, order_count)
if water > 0:
d['Water'] = (water, drink.count(countwater)) # d[menu_item] = (cost, order_count)
if lemonade > 0:
d['Lemonade'] = (lemonade, drink.count(countlemonade)) # d[menu_item] = (cost, order_count)
d_lst = sorted(d.items(), key=lambda x:x[1], reverse = True)
for item in d_lst:
print("Item: %s Cost: $%.2f OrderCount: %i "%(item[0], item[1][0], item[1][1]))
Upvotes: 0
Views: 365
Reputation: 229
A more complete code based on your specific question, run it as is and reply if it achieves your specific task:
#!/usr/bin/python
def main():
#welcome statement
print("Welcome to Deluxe Cafe")
print("Please select by entering the selections from the menu below.")
print("-------------------------------------------------------------------------------")
#drinks_dict: key = number;;; value = (name of the drink, cost, order_count)
drinks_dict = {'1': ['Coffee', 2.00,0], '2': ['Juice', 2.00, 0], '3': ['Soda', 1.50, 0], '4': ['Water', 1.00, 0], '5': ['Lemonade', 1.50, 0]}
#ask for customer input
while True:
for k,v in sorted(drinks_dict.items()):
print("%s. %s [%.2f]"%(k, v[0], v[1]))
order = raw_input("Please enter your selection: ")
if order in drinks_dict:
print("1x %s added "%drinks_dict[order][0])
drinks_dict[order][2] += 1
more_orders = raw_input("Do you want to order more? yes or no\n")
if more_orders == 'no':
break
else:
print('Please type either: 1 or 2 or 3 or 4 or 5')
drinks_list = []
drinks_list = sorted(drinks_dict.items(), key = lambda x: x[1][1], reverse = True)
if drinks_list:
print("Cafe Receipt")
for item in drinks_list:
if item[1][2] > 0:
print("Item:%s;;; Cost:%.2f;;; OrderCount:%i "%(item[1][0], item[1][1], item[1][2]))
if __name__ == '__main__':
main()
Upvotes: 0
Reputation: 229
your code is not properly indented. if I understand your question correctly the below code should do the job for you:
Create a dict:
d = {}
d['coffeec'] = (2.00, 2) # d[menu_item] = (cost, order_count)
d['juciec'] = (2.00, 1) # d[menu_item] = (cost, order_count)
d['sodac'] = (1.50, 10) # d[menu_item] = (cost, order_count)
d['waterc'] = (1.00, 20) # d[menu_item] = (cost, order_count)
d['lemonadec'] = (1.50, 10) # d[menu_item] = (cost, order_count)
so,
d = {'coffeec': (2.0, 2), 'waterc': (1.0, 20), 'lemonadec': (1.5, 10), 'juciec': (2.0, 1), 'sodac': (1.5, 10)}
Then reverse sort based on price:
d_lst = sorted(d.items(), key=lambda x:x[1][0], reverse = True)
so, the new d_lst is as below:
d_lst = [('coffeec', (2.0, 2)), ('juciec', (2.0, 1)), ('lemonadec', (1.5, 10)), ('sodac', (1.5, 10)), ('waterc', (1.0, 20))]
Then iterate over your new d_lst to print the receipt:
>>> for item in d_lst:
... print("Item:%s;;; Cost:%.2f;;; OrderCount:%i "%(item[0], item[1][0], item[1][1]))
...
Item:coffeec;;; Cost:2.00;;; OrderCount:2
Item:juciec;;; Cost:2.00;;; OrderCount:1
Item:lemonadec;;; Cost:1.50;;; OrderCount:10
Item:sodac;;; Cost:1.50;;; OrderCount:10
Item:waterc;;; Cost:1.00;;; OrderCount:20
Upvotes: 1