how would i get this code to work

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4804 Views - Last Post: 18 October 2012 - 03:32 PM Rate Topic: -----

#16 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: how would i get this code to work

Posted 14 October 2012 - 11:10 PM

Just loop through the whole orders. Simple implementation:
def orderFood():
    menu = dict([(1,(3.25,"Pricot Cake")),(2,(5.00,"Apple Cake")),(3,(7.25,"Hawaiian Pizza"))])
    orders = []
    print "Can I take your order? We currently have the following:"
    for k,v in menu.iteritems():
        print "Menu item #:{0} Name:{1} Price:{2}".format(k,v[1],v[0])
    print "Please input your order separated by spaces:"
    order = raw_input().split()
    order = map(int, order)
    lastPrice = 0
    for x in range(len(order)):
    	lastPrice = lastPrice + menu[order[x]][0]
    print "Here's your receipt!"
    for x in range(len(order)):
    	print "You ordered {0} for {1}".format(menu[order[x]][1],menu[order[x]][0])
    print "The total comes in at {0}".format(lastPrice)
    print "Thanks! Come again!"



Output:

Quote

>>> orderFood()
Can I take your order? We currently have the following:
Menu item #:1 Name:Pricot Cake Price:3.25
Menu item #:2 Name:Apple Cake Price:5.0
Menu item #:3 Name:Hawaiian Pizza Price:7.25
Please input your order separated by spaces:
1 2 3
Here's your receipt!
You ordered Pricot Cake for 3.25
You ordered Apple Cake for 5.0
You ordered Hawaiian Pizza for 7.25
The total comes in at 15.5
Thanks! Come again!

Was This Post Helpful? 0
  • +
  • -

#17 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,644
  • Joined: 16-October 07

Re: how would i get this code to work

Posted 15 October 2012 - 07:04 AM

Since we've got a pretty complete implementation on the table...

Still don't need a dict.
def menuOrder():
	menu = [('Pricot Chicken', 8.50), ('Meat Lovers', 13.50), ('Vegetarian', 13.50), ('Double Cheese', 13.50), ('Hawaiian', 13.50), ('Pepperoni', 8.50), ('Beef and Onion', 8.50), ('Ham and Cheese', 8.50), ('Tomato Margherita', 8.50), ('Classic Italian', 8.50), ('BBQ Chicken and Bacon', 8.50), ('Italian Veg', 8.50)]
	fmt, nfmt = '{0:<30} {1:>9} {2:>9}'.format, '{0:0.2f}'.format
	print "Can I take your order? We currently have the following:"
	print('\n'.join('{0:>3}. {1:<30} {2:>9}'.format(i+1, name, nfmt(price)) for i, (name, price) in enumerate(menu)))
	print("Please input your order separated by spaces:")
	order = sorted([ i for i in (int(s)-1 for s in raw_input().split() if s.isdigit()) if i in range(len(menu)) ])
	print("Your order:")
	print(fmt('Item', 'Price', 'Total'))
	total = 0.0
	for i in order:
		name, price = menu[i]
		total += price
		print(fmt(name, nfmt(price), nfmt(total)))
	print(fmt('Grand Total','', nfmt(total)))


Was This Post Helpful? 1
  • +
  • -

#18 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7650
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: how would i get this code to work

Posted 15 October 2012 - 07:08 AM

View PostlittlePilot, on 04 October 2012 - 03:59 AM, said:

is it possible to use something like this


That's what the interpreter is for. Try it and see!
Was This Post Helpful? 1
  • +
  • -

#19 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

Re: how would i get this code to work

Posted 18 October 2012 - 03:32 PM

View Postbaavgai, on 15 October 2012 - 07:04 AM, said:

Since we've got a pretty complete implementation on the table...

Still don't need a dict.
def menuOrder():
	menu = [('Pricot Chicken', 8.50), ('Meat Lovers', 13.50), ('Vegetarian', 13.50), ('Double Cheese', 13.50), ('Hawaiian', 13.50), ('Pepperoni', 8.50), ('Beef and Onion', 8.50), ('Ham and Cheese', 8.50), ('Tomato Margherita', 8.50), ('Classic Italian', 8.50), ('BBQ Chicken and Bacon', 8.50), ('Italian Veg', 8.50)]
	fmt, nfmt = '{0:<30} {1:>9} {2:>9}'.format, '{0:0.2f}'.format
	print "Can I take your order? We currently have the following:"
	print('\n'.join('{0:>3}. {1:<30} {2:>9}'.format(i+1, name, nfmt(price)) for i, (name, price) in enumerate(menu)))
	print("Please input your order separated by spaces:")
	order = sorted([ i for i in (int(s)-1 for s in raw_input().split() if s.isdigit()) if i in range(len(menu)) ])
	print("Your order:")
	print(fmt('Item', 'Price', 'Total'))
	total = 0.0
	for i in order:
		name, price = menu[i]
		total += price
		print(fmt(name, nfmt(price), nfmt(total)))
	print(fmt('Grand Total','', nfmt(total)))




is there a way to make it so u can only type in the same amount of pizzas as you inputed before in how meny pizzas you want... i was just wondering if there was a easy way to loop it to only work till the amount is reached. if you get what im asking... lol
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2