# how would i get this code to work

• (2 Pages)
• 1
• 2

## 18 Replies - 5344 Views - Last Post: 18 October 2012 - 03:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294207&amp;s=75da0bcdb66de516aea4df72f792f867&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 fromTheSprawl

• Monomania

Reputation: 513
• Posts: 2,063
• 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

• Dreaming Coder

Reputation: 6071
• Posts: 13,150
• 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

• Pancakes!

Reputation: 8430
• Posts: 14,527
• Joined: 19-March 11

## Re: how would i get this code to work

Posted 15 October 2012 - 07:08 AM

littlePilot, 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

• New D.I.C Head

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

## Re: how would i get this code to work

Posted 18 October 2012 - 03:32 PM

baavgai, 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }