如何检查用户是否输入了有效值?如果正确输入,我该如何将该值附加到列表中?

时间:2018-10-01 00:40:26

标签: python python-3.x input while-loop

menu_items = ["French fries", "1/4 pound burger", "1/4 pound chesseburger", "1/2 pound burger", "1/2 pound cheeseburger", "Medium pizza", "Meidum pizza with extra toppings", "Large pizza", "Large pizza with extra toppings", "Garlic bread"]
menu_prices = ["$2.00", "$5.00", "$5.55", "$7.00", "$7.50", "$9.00", "$11.00", "$12.00", "$14.50", "$4.50"]
menu_codes = ["ff", "bb", "ccb", "vb", "vcb", "mp", "mpx", "lp", "lpx", "gb"]

order = input(str("Write the item code of the food you wish to buy: "))
quantity = input(int("How many units of the previous food do want? "))

if order == "ff" or "bb" or "ccb" or "vb" or "vcb" or "mp" or "mpx" or "lp" or "lpx" or "gb":
    print("The item code was inputted correctly")
else:
    print("The item code was inputted incorrectly")

if quantity == (0 < 100):
    print("Quantity entered is inside acceptable range")
else:
    print("Quantity entered is out of range")

这是代码列表,应该是餐馆的菜单。用户应该只能输入列表中的项目(menu_codes),并且应拒绝输入任何其他值。并且数量应仅在1-100之间,并且不允许有任何其他差异。但是由于某种原因,我的“ while”循环不起作用,并且总是打印出相同的东西,这是我的代码输出的示例。

Write the item code of the food you wish to buy: this is not on the list
How many units of the previous food do want? 56
The item code was inputted correctly
Quantity entered is out of range

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助。数量应该是

if quantity <= 100:
   print("Quantity entered is inside acceptable range")

答案 1 :(得分:0)

order = input(str("Write the item code of the food you wish to buy: "))

quantity = input("How many units of the previous food do want::")


if order in menu_codes:
    print("The item code was inputted correctly")
else:
    print("The item code was inputted incorrectly")

if 0 <= int(quantity) <= 100:
    print("Quantity entered is inside acceptable range")
else:
    print("Quantity entered is out of range")