[英]Iterate through list in a list
1st year of CSE in HS. HS CSE 第一年。 Coding a pizza order program.编写比萨订购程序。 Teacher wants a separate function for computing cost.老师想要一个单独的 function 用于计算成本。 I am struggling with getting the program to add on the price of the toppings.我正在努力让程序增加配料的价格。 Each pizza is $13 and toppings are extra.每个比萨饼是 13 美元,浇头是额外的。 Goal is to have it charge for a plain pizza (empty list), pizza with toppings, or no pizza at all.目标是对普通披萨(空单)、有配料的披萨或根本不收费的披萨收费。
def pizza_cost(pizza):
global total
total += 13.00
for y in pizza:
if (y == "Pepperoni"):
total += 1.00
order = input("Would you like to make an order? (y or n)")
while order != "n":
pizza_order = input("Would you like a pizza? (y or n)")
pizza = []
while pizza_order.upper() != "N":
if pizza_order.upper() == "Y":
topping_option = input("Would you like to add a topping? (y or n)")
while topping_option.upper() != "N":
topping_list = []
topping = input("What topping would you like? Pepperoni (p), Mushroom (m)")
if topping.upper() == "P":
topping_list.append("Pepperoni")
elif topping.upper() == "M":
topping_list.append("Mushroom")
topping_option = input("Would you like to add another topping? (y or n)")
pizza.append(topping_list)
pizza_order = input("Would you like another pizza? (y or n)")
for x in pizza:
pizza_cost(pizza)
# Example order (Two plain pizzas and pizza with double-pepperoni)
([], [], ["Pepperoni", "Pepperoni"])
The only problem in your code is that you are passing the list of pizza lists pizza
, instead of a single pizza x
, to the pizza_cost
function, at the end of your code:您的代码中唯一的问题是,您将披萨列表的列表pizza
而不是单个披萨x
传递给代码末尾的pizza_cost
function:
for x in pizza:
pizza_cost(pizza)
If you pass x
instead, your whole code will do what you want;) !如果你通过x
代替,你的整个代码将做你想做的事;)!
for x in pizza:
pizza_cost(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.