在数组中选择字符串变量-Python

在数组中选择字符串变量-Python,python,arrays,python-3.x,Python,Arrays,Python 3.x,我正绞尽脑汁从数组中挑选一组字符串变量 代码: 数组中的项: 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),("Sata

我正绞尽脑汁从数组中挑选一组字符串变量

代码:

数组中的项:

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)]
for n in range(numPizza):
    pizza = pizza + [int(input("Choose a pizza: "))]
选择阵列中的比萨饼:

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)]
for n in range(numPizza):
    pizza = pizza + [int(input("Choose a pizza: "))]
对于所选比萨饼的总价:

for selected in pizza:
    total_price += pizzas_with_prices[selected][1]
    print("$%s" % (total_price))
我在获取阵列中选定的比萨名称时遇到问题,但我可以获取选定比萨的总价。 谢谢你的帮助

编辑:

全部代码:

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()
pizzas,价格=[(“夏威夷”,8.5),(“蔬菜豪华”,8.5),(“火腿和奶酪”,8.5),
(“Super Supreme”,8.5)、(“海鲜豪华”,8.5),
(“肉食爱好者”,第11.5节)、(“辣”和“辣”,第11.5节)、(“烧烤鸡肉和培根”,第11.5节),
(“沙爹鸡”,11.5)]
def菜单():
打印(“交货还是提货?”)
打印()
打印(“1]交付($5费用)”)
打印(“2]拾取”)
打印()
选项=int(输入(“>>”)
如果选项<1或选项>2:
打印(“仅1或2”)
打印()
如果选项==1:
customerName=输入(“输入客户名称:”)
customerAddress=输入(“输入客户地址:”)
customerPhone=输入(“输入您的电话号码:”)
打印()
打印(“谢谢”,customerName,“客户地址是”,customerAddress,“客户电话号码是”,customerPhone)
打印()
订单批萨()
如果选项==2:
customerName=输入(“输入客户名称:”)
打印()
订单批萨()
def orderPizza():
numPizza=0
全球比萨价格
Flag=True
而国旗:
尝试:
numPizza=int(输入(“您想要多少个比萨饼?(最多7个):”)
如果numPizza==0或numPizza>7:
打印(“选择不正确,请重试”)
其他:
Flag=False
除值错误外:
打印(“不是数字,请重试”)
打印()
对于指数,列举比萨饼(比萨饼价格):
打印(“%d%s:$%s%”(索引,比萨饼[0],比萨饼[1]))
比萨饼=[]
对于范围内的n(numPizza):#覆盖从0到9的值
比萨饼=比萨饼+[int(输入(“选择比萨饼”)]
印刷品(比萨饼)
总价=0
对于选定的比萨饼:
总价格+=比萨饼价格[选定][1]
打印($%s%%(总价))
菜单()

我认为变量pizza必须是一个列表,而不是整数。不确定您是否在上面的代码段中没有指出它,或者它是否是一个错误。但这应该行得通-

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)]
selected_pizzas = []
for n in range(len(pizzas_with_prices)):
    pizza = int(input("Choose a Pizza:"))
    selected_pizzas.append(pizza)

pizza_price=0.0
for n in selected_pizzas:
    pizza_price += pizzas_with_prices[n][1]
这不是你要找的吗

在新代码中,缺少的是:

total_price = 0
for selected in pizza:
    total_price += pizzas_with_prices[selected][1]
    **print("%s" % (pizzas_with_prices[selected][0]))**
    print("$%s" % (total_price))

在我看来,最好是使用字典来查询价格,这样你就不必在整个价格列表中循环。然后使用sum函数:

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)

您使用的是什么语言(不是Java或C/C++)?抱歉,忘了提及Python将更改标题。谢谢
pizzas\u with_prices[selected][0]
没有打印它。您能澄清您的问题吗?您的示例代码没有尝试检索比萨饼标签字符串,但您说这是您遇到问题的部分。您尝试了什么,但什么没有按照您的预期工作?您从哪里获得了
[0]