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

2024-10-02 12:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正绞尽脑汁从数组中挑选一组字符串变量。你知道吗

代码:

数组中的项:

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()

Tags: andinforinputwithpricetotalprices
2条回答

我认为变量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)

相关问题 更多 >

    热门问题