I am struggling to get my head around picking out a group of string variables out of an array.
Code:
The items in the array:
pizzas_with_prices = [("Hawaiian", 8.5), ("Veg Deluxe", 8.5), ("Ham and Cheese", 8.5),("Super Supreme", 8.5), ("Seafood Deluxe", 8.5),("Meatlovers", 11.5), ("Hot 'n' Spicy", 11.5), ("BBQ Chicken and Bacon", 11.5),("Satay Chicken", 11.5)]
Selecting the pizzas in the array:
for n in range(numPizza):
pizza = pizza + [int(input("Choose a pizza: "))]
For total price of the pizzas selected:
for selected in pizza:
total_price += pizzas_with_prices[selected][1]
print("$%s" % (total_price))
I am having trouble with getting the names of the pizzas selected in the array but I can get the total prices of the pizzas selected. Thanks for any of the help!
Edit:
Whole Code:
pizzas_with_prices = [("Hawaiian", 8.5), ("Veg Deluxe", 8.5), ("Ham and Cheese", 8.5),
("Super Supreme", 8.5), ("Seafood Deluxe", 8.5),
("Meatlovers", 11.5), ("Hot 'n' Spicy", 11.5), ("BBQ Chicken and Bacon", 11.5),
("Satay Chicken", 11.5)]
def menu():
print("Delivery or Pickup?")
print()
print("1] Delivery ($5 charge)")
print("2] Pickup")
print()
option = int(input(">>"))
if option < 1 or option > 2:
print("Only 1 or 2")
print()
if option == 1:
customerName = input("Enter customers name: ")
customerAddress = input("Enter customer Address: ")
customerPhone = input("Enter your phone number: ")
print()
print("Thank you", customerName, "Customers Address is", customerAddress, "and customers phone number is", customerPhone)
print()
orderPizza()
if option == 2:
customerName = input("Enter customers name: ")
print()
orderPizza()
def orderPizza():
numPizza=0
global pizzas_with_prices
Flag = True
while Flag:
try:
numPizza= int(input("How many Pizzas do you want? (MAX 7): "))
if numPizza ==0 or numPizza > 7:
print("Not a correct choice, Try again")
else:
Flag = False
except ValueError:
print("Not a number, Try again")
print()
for index, pizza in enumerate(pizzas_with_prices):
print("%d %s: $%s" % (index, pizza[0], pizza[1]))
pizza=[]
for n in range(numPizza): #covers values from 0 to 9
pizza = pizza + [int(input("Choose a pizza: "))]
print(pizza)
total_price = 0
for selected in pizza:
total_price += pizzas_with_prices[selected][1]
print("$%s" % (total_price))
menu()
It's better IMO to use a dictionary for prices, so you don't have to loop to the whole list for the price. Then use the sum function:
pizzas_with_prices = {'pizza1': 10, 'pizza2': 15}
selected_pizzas = []
# create the list of selected_pizzas with your code
# eg: selected_pizzas = ['pizza1', 'pizza2', 'pizza1']
pizza_price = sum(pizzas_with_prices[pizza] for pizza in pizza_list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With