how would i get this code to work

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 littlePilot  Icon User is offline

  • New D.I.C Head

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

how would i get this code to work

Post icon  Posted 04 October 2012 - 12:38 AM

this is a code i am trying to add into my main code and i want to do somthing like this (1 = paricot) so i can just go print ("1") then is comes up with (paricot) but i dont know how to do that and i have looked but cant find anthing saying what to do....
("""Gourmet

1 = pricot Chicken 13.50
2 = Meat Lovers 13.50
3 = Vegetarian 13.50
4 = Double Cheese 13.50
5 = Hawaiian 13.50

Regular
6 = Pepperoni $8.50
7 = Beef and Onion $8.50
8 = Ham and Cheese $8.50
9 = Tomato Margherita $8.50
10 = Classic Italian $8.50
11 = BBQ Chicken and Bacon $8.50
12 = Italian Veg $8.50 
 
""")

def pizzas_you_want ():
    pizza1, pizza2, pizza3, pizza4, pizza5 = [int(x) for x in input("what pizzas do u want?").split()]



if num_pizzas==1:
    print (pizza1 + d_or_p)
elif num_pizzas==2:
    print (pizza1 + pizza2 + d_or_p)
elif num_pizzas==3:
        print (pizza1 + pizza2 + pizza3 + d_or_p)
elif num_pizzas==4:
        print (pizza1 + pizza2 + pizza3 + pizza4 + d_or_p)
elif num_pizzas==5:
            print (pizza1 + pizza2 + pizza3 + pizza4 + pizza5 + d_or_p)





heres my whole code if anyone wants to give me some tips that will be a big help since i am new to programing thanks :)

def Pizzas ():
    #Regular
    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
    
    #Gourmet
    pricot_Chicken = 13.50
    Meat_Lovers = 13.50
    Vegetarian = 13.50
    Double_Cheese = 13.50
    Hawaiian = 13.5

def contact_stuff ():
    #contact stuff
    name = (input("what is your order name"))
    Contact = (input("what is your phone number"))
    Address = (input("what is your address"))
    d_or_p =input("is this order Delivery? (yes or no)").lower()
    if d_or_p == 'yes':
        d_or_p= 3
    elif d_or_p == 'no':
        d_or_p= 0  
        
    




def Pizza_code_how_meny_pizzas ():
    #now for the pizza crap ://>
    pass
while True:
    try:
        num_pizzas= int (input('how meny pizzas do u want to buy?'))
        if num_pizzas <= 5:
            break
        elif num_pizzas >= 6:
            print ("Please enter a number under 5")
            pass
    except ValueError:
        pass
    print ("dont make me hurt u O.o")
    
    


print ()
("""Gourmet

1 = pricot Chicken 13.50
2 = Meat Lovers 13.50
3 = Vegetarian 13.50
4 = Double Cheese 13.50
5 = Hawaiian 13.50

Regular
6 = Pepperoni $8.50
7 = Beef and Onion $8.50
8 = Ham and Cheese $8.50
9 = Tomato Margherita $8.50
10 = Classic Italian $8.50
11 = BBQ Chicken and Bacon $8.50
12 = Italian Veg $8.50 
 
""")

def pizzas_you_want ():
    pizza1, pizza2, pizza3, pizza4, pizza5 = [int(x) for x in input("what pizzas do u want?").split()]



if num_pizzas==1:
    print (pizza1 + d_or_p)
elif num_pizzas==2:
    print (pizza1 + pizza2 + d_or_p)
elif num_pizzas==3:
        print (pizza1 + pizza2 + pizza3 + d_or_p)
elif num_pizzas==4:
        print (pizza1 + pizza2 + pizza3 + pizza4 + d_or_p)
elif num_pizzas==5:
            print (pizza1 + pizza2 + pizza3 + pizza4 + pizza5 + d_or_p)
  

This post has been edited by atraub: 04 October 2012 - 11:48 AM


Is This A Good Question/Topic? 2
  • +

Replies To: how would i get this code to work

#2 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 04 October 2012 - 12:49 AM

Use a dictionary of tuples!
>>> menu = [(1,'cake'),(2,'chocolate cake'),(3,'strawberry cake'),(4,'awesome cake')];
>>> menuList = dict(menu)
>>> menuList[4]
'awesome cake'


Awesome! ^^

I never thought reading books could be fun!(Dictionaries in Python)
Was This Post Helpful? 1
  • +
  • -

#3 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 04 October 2012 - 01:59 AM

View PostfromTheSprawl, on 04 October 2012 - 12:49 AM, said:

Use a dictionary of tuples!
>>> menu = [(1,'cake'),(2,'chocolate cake'),(3,'strawberry cake'),(4,'awesome cake')];
>>> menuList = dict(menu)
>>> menuList[4]
'awesome cake'


Awesome! ^^

I never thought reading books could be fun!(Dictionaries in Python)


is it possible to use something like this

menu = [(1,'pricot Chicken'),(2, 'Meat Lovers'),(3, 'Vegetarian'),(4, 'Double Cheese'),(5, 'Hawaiian'), (6, 'Pepperoni'), (7, 'Beef and Onion'), (8, 'Ham and Cheese'),(9, 'Tomato Margherita'),(10, 'Classic Italian'),(11, 'BBQ Chicken and Bacon'),(12,'Italian Veg')]

menuList = dict(menu)

pizza1 = int(input("what pizza do u want")) 

print (menuList [pizza1])


and then it takes the number the user input'ed and put in the pizza they ordered? from the menu? thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 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 04 October 2012 - 02:16 AM

Yep yep, pretty much. Try it in your Python IDE, if in doubt. ^^

>>> menu = [(1,'pricot Chicken'),(2, 'Meat Lovers'),(3, 'Vegetarian'),(4, 'Double Cheese'),(5, 'Hawaiian'), (6, 'Peppero
ni'), (7, 'Beef and Onion'), (8, 'Ham and Cheese'),(9, 'Tomato Margherita'),(10, 'Classic Italian'),(11, 'BBQ Chicken an
d Bacon'),(12,'Italian Veg')]
>>> menuList = dict(menu)
>>> def orderStuff(orderNumber):
...     print "You've ordered ",menuList[orderNumber],"! Thanks! :)/>"
...
>>> orderStuff(2)
You've ordered  Meat Lovers ! Thanks! :)/>


Also, I just used the Tuples to Dictionary example because it seems more compact to me and that's the thing I remember, I'm sure there's another way to set up your Dictionary. Read it on the same link I gave to you above.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: how would i get this code to work

Posted 04 October 2012 - 05:32 AM

However, a dictionary has no order. To show 1..12 you'd have to order the keys.

It's also unnecessary. You really only need a list. The position in the list is intrinsic:
>>> menu = ['pricot Chicken', 'Meat Lovers', 'Vegetarian', 'Double Cheese', 'Hawaiian', 'Pepperoni', 'Beef and Onion', 'Ham and Cheese', 'Tomato Margherita', 'Classic Italian', 'BBQ Chicken and Bacon', 'Italian Veg']
>>> 
>>> print('\n'.join('{0:>3}. {1}'.format(i+1, v) for i,v in enumerate(menu)))
  1. pricot Chicken
  2. Meat Lovers
  3. Vegetarian
  4. Double Cheese
  5. Hawaiian
  6. Pepperoni
  7. Beef and Onion
  8. Ham and Cheese
  9. Tomato Margherita
 10. Classic Italian
 11. BBQ Chicken and Bacon
 12. Italian Veg
>>> 
>>> pizza1 = int(input("what pizza do u want")) - 1
what pizza do u want3
>>> print(menu [pizza1]) 
Vegetarian
>>> 



Note, should pricot Chicken be Apricot Chicken? If not, I really want to know what a pricot is.

This post has been edited by baavgai: 04 October 2012 - 05:32 AM

Was This Post Helpful? 1
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how would i get this code to work

Posted 04 October 2012 - 06:42 AM

I'm rather partial to a merge of the two approaches:
>>> menu = dict([('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")])

>>> print('\n'.join('{0:>3}. {1}.....{2}'.format(index+1, key, menu[key]) for index,key in enumerate(sorted(menu))))
  1. BBQ Chicken and Bacon.....8.50
  2. Beef and Onion.....8.50
  3. Classic Italian.....8.50
  4. Double Cheese.....13.50
  5. Ham and Cheese.....8.50
  6. Hawaiian.....13.50
  7. Italian Veg.....8.50
  8. Meat Lovers.....13.50
  9. Pepperoni.....8.50
 10. Pricot Chicken.....8.50
 11. Tomato Margherita.....8.50
 12. Vegetarian.....13.50


Notice that I used sorted to sort the keys into alphabetical order, but enumerate still has the same behavior on a dictionary as a list.

SIDE NOTE:
Funny side note, I googled pricot chicken and this page is currently the 4th result! littlePilot is moving us up the search rankings :D

He definitely deserves a Posted Image for that

EDIT 2:
There are some really good techniques being showcased here. Featured!

This post has been edited by atraub: 04 October 2012 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#7 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 09 October 2012 - 01:14 AM

View Postatraub, on 04 October 2012 - 06:42 AM, said:

I'm rather partial to a merge of the two approaches:
>>> menu = dict([('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")])

>>> print('\n'.join('{0:>3}. {1}.....{2}'.format(index+1, key, menu[key]) for index,key in enumerate(sorted(menu))))
  1. BBQ Chicken and Bacon.....8.50
  2. Beef and Onion.....8.50
  3. Classic Italian.....8.50
  4. Double Cheese.....13.50
  5. Ham and Cheese.....8.50
  6. Hawaiian.....13.50
  7. Italian Veg.....8.50
  8. Meat Lovers.....13.50
  9. Pepperoni.....8.50
 10. Pricot Chicken.....8.50
 11. Tomato Margherita.....8.50
 12. Vegetarian.....13.50


Notice that I used sorted to sort the keys into alphabetical order, but enumerate still has the same behavior on a dictionary as a list.

SIDE NOTE:
Funny side note, I googled pricot chicken and this page is currently the 4th result! littlePilot is moving us up the search rankings :D

He definitely deserves a Posted Image for that

EDIT 2:
There are some really good techniques being showcased here. Featured!


so how would i use that in a
print
so i can just go
print (menu [pizza1)
and then
print (("that comes to a total of") *****)) 
is there a way to do this easily?



and woot! for number 4 :P
Was This Post Helpful? 0
  • +
  • -

#8 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 11 October 2012 - 08:13 PM

and also is there a easier way to do this print



num_pizzas = int(input(how many pizzas do u want?))



if num_pizzas == '1':
    pizza1 = int(input("what pizza do u want"))
    print(menu [pizza1])

elif num_pizzas == '2':
    pizza1, pizza2 = [int(x) for x in input("what pizzas do u want?").split()]
    print((menu [pizza1]) , (menu [pizza2]))

elif num_pizzas == '3':
    pizza1, pizza2, pizza3 = [int(x) for x in input("what pizzas do u want?").split()]
    print((menu [pizza1]) , (menu [pizza2]) , (menu [pizza3]))

elif num_pizzas == '4':
    pizza1, pizza2, pizza3, pizza4 = [int(x) for x in input("what pizzas do u want?").split()]
    print((menu [pizza1]) , (menu [pizza2]) , (menu [pizza3]) , (menu [pizza4]))

elif num_pizzas == '5':
    pizza1, pizza2, pizza3, pizza4, pizza5 = [int(x) for x in input("what pizzas do u want?").split()]
    print((menu [pizza1]) , (menu [pizza2]) , (menu [pizza3]) , (menu [pizza4]) , (menu [pizza5]))


Was This Post Helpful? 0
  • +
  • -

#9 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 11 October 2012 - 08:21 PM

Holy cow, are those pizza combos? I'm pretty sure you can also do a dictionary of lists too.
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how would i get this code to work

Posted 11 October 2012 - 08:22 PM

this was a fun approach I came up with:
>>> menu = ['pricot Chicken', 'Meat Lovers', 'Vegetarian', 'Double Cheese', 'Hawaiian', 'Pepperoni', 'Beef and Onion', 'Ham and Cheese', 'Tomato Margherita', 'Classic Italian', 'BBQ Chicken and Bacon', 'Italian Veg']

>>>order = [menu[int(x)] for x in input("what pizzas do u want?").split()]
what pizzas do u want?4 7 3
>>> print("So you'll have: "+", ".join(order)+" that's great order!")
So you'll have: Hawaiian, Ham and Cheese, Double Cheese that's great order!
>>> 



EDIT:
If you want pricot chicken to be 1 rather than 0, you'll need to change the order assignment to:
order = [menu[int(x)-1] for x in input("what pizzas do u want?").split()]

This post has been edited by atraub: 12 October 2012 - 11:03 AM

Was This Post Helpful? 1
  • +
  • -

#11 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 11 October 2012 - 08:25 PM

That looks awesome! That what you wanted? ^^
Was This Post Helpful? 0
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how would i get this code to work

Posted 11 October 2012 - 08:31 PM

View PostfromTheSprawl, on 11 October 2012 - 11:25 PM, said:

That looks awesome! That what you wanted? ^^

I'm not sure if this is a compliment or an underhanded insult but... thanks? I think.
Was This Post Helpful? 0
  • +
  • -

#13 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 12 October 2012 - 12:07 AM

It's a compliment dude. :)

I'm particularly impressed of the conversion of the input to array then using it on the list comprehension.
Was This Post Helpful? 0
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how would i get this code to work

Posted 12 October 2012 - 11:05 AM

We're now number 2 for pricot chicken lol
Was This Post Helpful? 0
  • +
  • -

#15 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 14 October 2012 - 10:35 PM

thanks for all the help guys.... :) but i was wondering if theres a easy way to get a total so
"So you'll have: Hawaiian, Ham and Cheese, Double Cheese that's great order! that comes to a total of $35.50" :)?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2