目标:
在用户输入是否要添加另一个新项目之后,我希望代码从newItem
输入行重新开始,这样我就不必只继续if
火车了,最终给它加了一个盖子。
下面列出的是我到目前为止编写的代码。
shopping_cart_menu = []
shopping_cart_prices = []
print("Welcome to the shopping cart program. Please select an option from the menu below.")
print("*******************")
print("01: Add a new Item")
print("02: Display Cart contents")
print("03: Remove Item")
print("04: Compute Total")
print("05: Quit")
print("*******************")
menuAnswer = input(" ")
if menuAnswer == "01":
newItem = input("What item would you like to add? ")
shopping_cart_menu.append({newItem})
followUp = input("Would you like to add another item? Y/N: ")
if answer = "Y"
答案 0 :(得分:1)
使用while循环将允许您循环if语句。 “ followup = followup.upper()”行也将大写输入。
shopping_cart_menu = []
shopping_cart_prices = []
print("Welcome to the shopping cart program. Please select an option from the menu below.")
print("*******************")
print("01: Add a new Item")
print("02: Display Cart contents")
print("03: Remove Item")
print("04: Compute Total")
print("05: Quit")
print("*******************")
menuAnswer = input(" ")
if menuAnswer == "01":
followup = "Y"
while followup == "Y":
newItem = input("What item would you like to add? ")
shopping_cart_menu.append({newItem})
followup = input("Would you like to add another item? Y/N: ")
followup = followup.upper()
答案 1 :(得分:1)
以下是使用while
循环的方法:
menuAnswer = input(" ")
if menuAnswer == "01":
while True:
newItem = input("What item would you like to add? ")
shopping_cart_menu.append({newItem})
followUp = input("Would you like to add another item? Y/N: ")
if answer != "Y":
break