带有尾随值的嵌套while循环
我这个程序的目的是模拟一个虚拟的麦当劳收银员。程序的第一部分让收银员可以接收一个顾客的订单。得益于循环的使用,顾客可以点任意数量的餐品。最后,程序会显示税费、小计和最终账单,以及顾客支付的金额和找回的零钱。现在我希望我的程序能完成第一部分的所有功能,但我需要收银员能够为排队的多个顾客服务。我尝试把两个循环嵌套在一起,但我不知道哪里出了问题。有人能帮我吗?顺便说一下,第一部分的功能是完全正常的。
第一部分
num1 = 4.87
num2 = 5.03
num3 = 5.50
num4 = 9.45
num5 = 1.29
num6 = 2.19
num7 = 2.29
itemnum = 0
Subtotal = 0
tax = 0.0565
amtgiven = 0
change = 0
quantity = 0
foodprice = 0
Totalprice1 = 0
Totalprice2 = 0
Totalprice3 = 0
Totalprice4 = 0
Totalprice5 = 0
Totalprice6 = 0
Totalprice7 = 0
billtax = 0
finalbill = 0
change = 0
print "Welcome to Virtual McDonald's!" "Would you like to order a food item"
print "Item: Meal/tem: Price:"
print "1 Big Mac Meal 4.87"
print "2 Quarter Pounder Meal 5.03"
print "3 Chicken Nuggets Meal (5 piece) 5.50"
print "4 ChickenNuggets Meal (10 piece) 9.45"
print "5 Apple Pie 1.29"
print "6 Large Drink 2.19"
print "7 Large Fries 2.29"
itemnum = input("Enter the item you would like to purchase! ")
quantity = input("How many of this item do you want?")
while itemnum != -1:
if itemnum == 1:
foodprice = quantity * num1
Totalprice1 = foodprice
if itemnum == 2:
foodprice = quantity * num2
Totalprice2 = foodprice
if itemnum == 3:
foodprice = quantity * num3
Totalprice3 = foodprice
if itemnum == 4:
foodprice == quantity * num4
Totalprice4 = foodprice
if itemnum == 5:
foodprice = quantity * num5
Totalprice5 = foodprice
if itemnum == 6:
foodprice == quantity * num6
Totalprice6 = foodprice
if itemnum == 7:
foodprice = quantity * num7
Totalprice7 = foodprice
subtotal = Totalprice1 + Totalprice2 + Totalprice3 + Totalprice4 + Totalprice5 + Totalprice6 + Totalprice7
billtax = subtotal * tax
finalbill = subtotal + billtax
itemnum = input("Enter the item you would like to purchase! ")
quantity = input("How many of this item do you want?")
print "Your total bill without tax is... ", round(subtotal,2)
print "Your total tax is... ", round(billtax,2)
print "Your final bill is... ", round(finalbill,2)
amtgiven = input ("How much do you want to pay with?")
change = amtgiven - finalbill
print "Your change is... ", round(change,2)
第二部分
num1 = 4.87
num2 = 5.03
num3 = 5.50
num4 = 9.45
num5 = 1.29
num6 = 2.19
num7 = 2.29
itemnum = 0
Subtotal = 0
tax = 0.0565
amtgiven = 0
change = 0
quantity = 0
foodprice = 0
Totalprice1 = 0
Totalprice2 = 0
Totalprice3 = 0
Totalprice4 = 0
Totalprice5 = 0
Totalprice6 = 0
Totalprice7 = 0
billtax = 0
finalbill = 0
change = 0
customer = 0
print "Welcome to Virtual McDonald's!" "Would you like to order a food item"
print "Item: Meal/tem: Price:"
print "1 Big Mac Meal 4.87"
print "2 Quarter Pounder Meal 5.03"
print "3 Chicken Nuggets Meal (5 piece) 5.50"
print "4 ChickenNuggets Meal (10 piece) 9.45"
print "5 Apple Pie 1.29"
print "6 Large Drink 2.19"
print "7 Large Fries 2.29"
customer = raw_input ("Would you like to order? (If not type No)")
while customer != "No":
while itemnum != -1:
itemnum = input("Enter the item you would like to purchase! ")
quantity = input("How many of this item do you want? ")
if itemnum == 1:
foodprice = quantity * num1
Totalprice1 = Totalprice1 + foodprice
if itemnum == 2:
foodprice = quantity * num2
Totalprice2 = Totalprice2 + foodprice
if itemnum == 3:
foodprice = quantity * num3
Totalprice3 = Totalprice3 + foodprice
if itemnum == 4:
foodprice = quantity * num4
Totalprice4 = Totalprice4 + foodprice
if itemnum == 5:
foodprice = quantity * num5
Totalprice5 = Totalprice5 + foodprice
if itemnum == 6:
foodprice = quantity * num6
Totalprice6 = Totalprice6 + foodprice
if itemnum == 7:
foodprice = quantity * num7
Totalprice7 = Totalprice7 + foodprice
itemnum = input("Enter the item you would like to purchase! ")
quantity = input("How many of this item do you want? ")
subtotal = Totalprice1 + Totalprice2 + Totalprice3 + Totalprice4 + Totalprice5 + Totalprice6 + Totalprice7
billtax = subtotal * tax
finalbill = subtotal + billtax
print "Your total bill without tax is... ", round(subtotal,2)
print "Your total tax is... ", round(billtax,2)
print "Your final bill is... ", round(finalbill,2)
amtgiven = input ("How much do you want to pay with? ")
change = amtgiven - finalbill
print "Your change is... ", round(change,2)
customer = raw_input ("Would you like to order? (If not type No)")
#输出
当我运行第二部分程序时,输出结果是这样的:
Welcome to Virtual McDonald's!Would you like to order a food item
Item: Meal/tem: Price:
1 Big Mac Meal 4.87
2 Quarter Pounder Meal 5.03
3 Chicken Nuggets Meal (5 piece) 5.50
4 ChickenNuggets Meal (10 piece) 9.45
5 Apple Pie 1.29
6 Large Drink 2.19
7 Large Fries 2.29
Would you like to order? (If not type No) yes
Enter the item you would like to purchase! 1
How many of this item do you want? 2
Enter the item you would like to purchase! 1
How many of this item do you want? 4
Your total bill without tax is... 9.74
Your total tax is... 0.55
Your final bill is... 10.29
How much do you want to pay with? 11
Your change is... 0.71
Enter the item you would like to purchase!
(在输入了几个菜单项后,程序停止了循环,直接跳到账单部分。而且在账单之后,它没有询问用户是否还有其他顾客。)
当我运行第一部分时,输出结果是这样的:
Welcome to Virtual McDonald's!Would you like to order a food item
Item: Meal/tem: Price:
1 Big Mac Meal 4.87
2 Quarter Pounder Meal 5.03
3 Chicken Nuggets Meal (5 piece) 5.50
4 ChickenNuggets Meal (10 piece) 9.45
5 Apple Pie 1.29
6 Large Drink 2.19
7 Large Fries 2.29
Enter the item you would like to purchase! 1
How many of this item do you want?4
Enter the item you would like to purchase! 2
How many of this item do you want?1
Enter the item you would like to purchase! -1
How many of this item do you want?-1
Your total bill without tax is... 24.51
Your total tax is... 1.38
Your final bill is... 25.89
How much do you want to pay with? 26
Your change is... 0.11
我希望第二部分的输出和第一部分一样。除了在给顾客找回零钱后,我想让它执行另一个循环,询问用户是否还有其他顾客。如果用户输入“是”,那么程序就会重新开始,为下一个顾客服务。只有当没有其他顾客时,用户输入“否”来结束程序。(我去Python的命令行复制了这些输出结果,右边的数字是我输入的数字。)
1 个回答
好的,你的程序有一些地方可以改进,既有语法上的问题,也有逻辑上的问题。我已经帮你修正了这些问题,并且会解释我做了什么以及为什么这么做。首先,你需要再加一个 while
循环,这样用户才能选择是否还有其他顾客在排队。我创建了一个新的变量来处理这个新的 while
循环。下面是相关的代码:
nextcustomer = "yes"
while nextcustomer != "no":
amtgiven = 0
change = 0
quantity = 0
foodprice = 0
totalprice = 0
billtax = 0
finalbill = 0
itemnum = 0
在这个新循环里,我们需要添加一些变量,这些变量在每个顾客处理完后需要重置。循环外的全局变量应该是那些在不同顾客之间不需要改变的,比如食物的价格或税率。我们还需要添加找零和总价格等变量,我在之前的代码中已经做了这些。
另外,关于是否还有其他顾客的问题应该放在第一个 while
循环里,在处理完其他所有事情之后,这样程序才能知道是否需要再次循环。这个问题的代码如下:
while nextcustomer != "no":
(all of the code)
nextcustomer = raw_input("Is there another customer? (yes or no) ")
我还添加了一些中断语句,这样在第二个 while
循环中,如果没有这些中断,程序会一直循环下去,而无法询问用户是否想要下单。
我还做了一个改动,把你所有的 Totalprice1, Totalprice2
...变量合并成一个变量 totalprice
。因为你已经有了各个价格的变量(比如 num1, num2
...),所以只需要一个变量就可以了。每个选择下的计数器变量的代码看起来是这样的:
totalprice += foodprice
这相当于 totalprice = totalprice + foodprice
。这样写更简洁。此外,现在你甚至不需要一个 subtotal
变量了。
我还在用户选择的数字后面添加了一个 if
语句,用来检查他们是否输入了 -1,这样就不需要输入 -1 这个项目的金额了:
if itemnum == -1:
break
我相信还有一些其他的改动,我可能会回来再编辑一下,确保所有的内容都解释清楚了。下面是编辑后的完整程序代码:
num1 = 4.87
num2 = 5.03
num3 = 5.50
num4 = 9.45
num5 = 1.29
num6 = 2.19
num7 = 2.29
tax = 0.0565
customer = 0
nextcustomer = "yes"
while nextcustomer != "no":
amtgiven = 0
change = 0
quantity = 0
foodprice = 0
totalprice = 0
billtax = 0
finalbill = 0
itemnum = 0
print "Welcome to Virtual McDonald's!"
print "Item: Meal/item: Price:"
print "1 Big Mac Meal 4.87"
print "2 Quarter Pounder Meal 5.03"
print "3 Chicken Nuggets Meal (5 piece) 5.50"
print "4 ChickenNuggets Meal (10 piece) 9.45"
print "5 Apple Pie 1.29"
print "6 Large Drink 2.19"
print "7 Large Fries 2.29"
customer = raw_input ("Would you like to order? (If not type no)")
while customer != "no":
while itemnum != -1:
itemnum = input("Enter the item you would like to purchase! ")
if itemnum == -1:
break
quantity = input("How many of this item do you want? ")
if itemnum == 1:
foodprice = quantity * num1
totalprice += foodprice
elif itemnum == 2:
foodprice = quantity * num2
totalprice += foodprice
elif itemnum == 3:
foodprice = quantity * num3
totalprice += foodprice
elif itemnum == 4:
foodprice = quantity * num4
totalprice += foodprice
elif itemnum == 5:
foodprice = quantity * num5
totalprice += foodprice
elif itemnum == 6:
foodprice = quantity * num6
totalprice += foodprice
elif itemnum == 7:
foodprice = quantity * num7
totalprice += foodprice
billtax = totalprice * tax
finalbill = totalprice + billtax
print "Your total bill without tax is... ", round(totalprice,2)
print "Your total tax is... ", round(billtax,2)
print "Your final bill is... ", round(finalbill,2)
amtgiven = input("How much do you want to pay with? ")
change = amtgiven - finalbill
print "Your change is... ", round(change,2)
break
nextcustomer = raw_input("Is there another customer? (yes or no) ")
这样解决了你的问题吗?