我正在用python创建此程序,该程序会根据您选择的内容来计算总费用。 因此该程序是披萨递送,用户必须从菜单中选择自己选择的披萨。 我不能弄清楚的是,如何告诉python,无论用户的第一,第二和第三选择是什么,成本将是3美元,其余的将是5美元。 我在更多部门中的意思是,我该如何告诉python用户1st,2nd,3rd选择/输入= 3美元。
我尝试编写:menu [0:3] = 3,但这只是改变了数组/列表中的食物。 this is a sample of my code(not the full code)
答案 0 :(得分:0)
尝试此功能:
def price(choice):
if choice <= 3:
cost = 3
else:
cost = 5
return cost
price(4)
> 5
答案 1 :(得分:0)
您可以创建一个计数器变量来跟踪输入,在获取每个输入后将其递增。然后在代码的价格部分,如果计数器<4,则使用if语句将price设置为3,否则将price设置为5。
# Create counter variable
counter = 0
# Taking user input code here
counter = counter + 1
# Later when setting price in code
if counter < 4:
price = 3
else:
price = 5
希望这会给您一些想法,随时发表评论,并在需要时要求进一步的澄清!
答案 2 :(得分:0)
您应该执行以下操作:
for i in range(1, number_of_pizza+1):
if i < 4:
total_cost += 3
if i >= 4:
total_cost += 5
答案 3 :(得分:0)
代码:
i = 1
total_cost = 0
while(True):
choice = input("Please Enter Your Choice:")
if(i<4):
price = 3
total_cost = total_cost + price
else:
price = 5
total_cost = total_cost + price
i = i + 1
check = input("Do you want to enter another choice? (y/n)")
if(check=="N" or check="n" or check=="No" or check=="no"):
break
print("Total Cost : $",total_cost)
输出:
Please Enter Your Choice:tea
Do you want to enter another choice? (y/n)y
Please Enter Your Choice:coffe
Do you want to enter another choice? (y/n)y
Please Enter Your Choice:apple
Do you want to enter another choice? (y/n)n
Total Cost : $ 9
答案 4 :(得分:0)
根据我对您问题的理解,您希望将菜单项在列表中分隔为:
menu = ['Cheese', 'Vege delight', 'pineapple', 'pepperoni','meat lovers', 'butter chicken', 'spicy corn delight', 'veg slingshot']
,而不是您在图片中显示的方式。