【发布时间】:2016-02-10 06:08:31
【问题描述】:
我正在努力从数组中挑选出一组字符串变量。
代码:
数组中的项目:
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()
【问题讨论】:
-
您使用的是什么语言(不是 Java 或 C/C++)?
-
抱歉忘了说 Python 会改标题谢谢
-
pizzas_with_prices[selected][0]没有打印出来? -
你能澄清你的问题吗?您的示例代码不会尝试检索披萨标签字符串,但这就是您说您遇到问题的部分。您尝试了哪些方法,哪些没有按您的预期工作?
-
你从哪里得到
[0]位
标签: python arrays python-3.x