如何检查用户输入是否对 list/tuple 个比萨饼有效?
How to check if a users input is valid against a list/tuple of pizzas?
抱歉,如果之前有人问过这个问题,但我在其他问题中找不到确切的答案或足够接近的答案。我也是 Python 的新手。
我正在寻找一种方法来检查从元组中选择比萨饼时用户输入是否有效。 (例如,如果用户输入任何超过 11 的值,我希望程序重新启动该功能。)
到目前为止,这是我的程序的完整代码。
def amountFunction():
global pizzaAmount
pizzaAmount = []
while pizzaAmount is not int:
try:
pizzaAmount = int(input("Please input the amount of pizzas you would like - (a maximum of 5)"))
except ValueError:
print("Please input a value above 0 or less than 5")
except pizzaAmount ==0 or pizzaAmount > 5:
print("Please input a value above 0 or less than 5")
amountFunction()
else:
print("Your amount of pizzas is " + str(pizzaAmount))
break
amountFunction()
def selectFunction():
global pizzas_with_prices
pizzas_with_prices = [("BBQ Meatlovers", 8.5), ("Chicken Supreme", 8.5), ("Peri-Peri Chicken", 8.5),
("Vegorama", 8.5), ("Cheesy Bacon Hawaiian", 8.5), ("Beef and Onion", 8.5),
("Simply Cheese", 8.5), ("Ham and Cheese", 13.5), ("Hawaiian", 13.5),
("Veg Trio", 13.5), ("Peperoni", 13.5), ("Wedge", 13.5)]
for index, pizza in enumerate(pizzas_with_prices):
print("%d %s: $%s" % (index, pizza[0], pizza[1]))
global selected_pizza
selected_pizza=[]
for n in range(pizzaAmount):
while selected_pizza is not int:
try:
selected_pizza = selected_pizza + [int(input("Choose a pizza: "))]
except ValueError:
print("Please select a pizza on the list")
else:
break
global total_price
total_price = 0
for selected in selected_pizza:
total_price += pizzas_with_prices[selected][1]
selectFunction()
def totalFunction():
print("Here are your selected pizzas")
for selected in selected_pizza:
print("%s: $%s" % pizzas_with_prices[selected])
print("Here is the total price of pizzas:${}".format(total_price))
totalFunction()
有问题的函数是下面这个 - def selectFunction():
def selectFunction():
global pizzas_with_prices
pizzas_with_prices = [("BBQ Meatlovers", 8.5), ("Chicken Supreme", 8.5), ("Peri-Peri Chicken", 8.5),
("Vegorama", 8.5), ("Cheesy Bacon Hawaiian", 8.5), ("Beef and Onion", 8.5),
("Simply Cheese", 8.5), ("Ham and Cheese", 13.5), ("Hawaiian", 13.5),
("Veg Trio", 13.5), ("Peperoni", 13.5), ("Wedge", 13.5)]
for index, pizza in enumerate(pizzas_with_prices):
print("%d %s: $%s" % (index, pizza[0], pizza[1]))
global selected_pizza
selected_pizza=[]
for n in range(pizzaAmount):
while selected_pizza is not int:
try:
selected_pizza = selected_pizza + [int(input("Choose a pizza: "))]
except ValueError:
print("Please select a pizza on the list")
else:
break
global total_price
total_price = 0
for selected in selected_pizza:
total_price += pizzas_with_prices[selected][1]
selectFunction()
我将如何检查用户输入是否在 list/tuple 比萨饼上? (例如,如果用户为 selected_pizza 输入 999,我希望程序再次重复 selectFunction() 直到选择了有效的比萨饼,然后继续计算比萨饼的总价格,然后继续下一个函数 totalFunction() )
我试过使用 IndexError: 但我似乎不明白如何使用它,因为它似乎不起作用,要么跳过并给我一个错误,要么进入无限循环。本质上我需要一种处理索引错误的方法。
以下是目前发生的情况的示例。
Please input the amount of pizzas you would like - (a maximum of 5)2
Your amount of pizzas is 2
0 BBQ Meatlovers: .5
1 Chicken Supreme: .5
2 Peri-Peri Chicken: .5
3 Vegorama: .5
4 Cheesy Bacon Hawaiian: .5
5 Beef and Onion: .5
6 Simply Cheese: .5
7 Ham and Cheese: .5
8 Hawaiian: .5
9 Veg Trio: .5
10 Pepperoni: .5
11 Wedge: .5
Choose a pizza: 23456
Choose a pizza: 4235464
Traceback (most recent call last):
File "C:\Users\ilyas rosslan\Documents\Python Work\tester.py", line 45, in
<module>
selectFunction()
File "C:\Users\ilyas rosslan\Documents\Python Work\tester.py", line 42, in
selectFunction
total_price += pizzas_with_prices[selected][1]
IndexError: list index out of range
如果有人能提供帮助,我们将不胜感激。谢谢
伊利亚斯。
读取输入,验证它,然后将它添加到 selected_pizza
列表。
for n in range(pizzaAmount):
while True:
try:
selection = int(input("Choose a pizza: "))
if selection in range(len(pizza_with_prices)):
selected_pizza.append(selection)
break
else:
print("Please select one of the listed pizza numbers")
except ValueError:
print("Please select a pizza on the list")
while selected_pizza is not int:
不正确。该条件将始终为真,因为 selected_pizza
是一个列表,而列表不是 int
.
你也应该停止使用这么多全局变量。 amountFunction()
应该 return 金额,然后它应该作为参数传递给 selectFunction()
。
据我了解,我会这样做。我为 select 一个有效的比萨创建了一个单独的函数,然后添加到 select 许多比萨的函数。
def amountFunction():
try:
pizzaAmount = int(input("Please input the amount of pizzas you would like - (a maximum of 5)"))
if pizzaAmount > 0 and pizzaAmount <=5:
return pizzaAmount
else:
print("Please input a value above 0 or less than 5")
return amountFunction()
except ValueError:
return amountFunction()
except KeyboardInterrupt:
print("user aborted the program")
return 0
def selectOnePizza(pizzas_with_prices):
try:
selected_pizza = int(input("Choose a pizza: "))
if selected_pizza >= 0 and selected_pizza <len(pizzas_with_prices):
return selected_pizza
else:
print("Please input a value above {} or less than {}".format(0,len(pizzas_with_prices)))
return selectOnePizza(pizzas_with_prices)
except ValueError:
print("Please input a value above {} or less than {}".format(0,len(selected_pizza)))
return selectOnePizza(pizzas_with_prices)
except KeyboardInterrupt:
print("user aborted the program")
return -1
def selectManyPizzas(pizzas_with_prices, pizzaAmount):
for index, pizza in enumerate(pizzas_with_prices):
print("%d %s: $%s" % (index, pizza[0], pizza[1]))
selected_pizza=[]
for n in range(pizzaAmount):
selected_pizza.append(selectOnePizza(pizzas_with_prices))
return selected_pizza
def showSlectedPizzas(pizzas_with_prices, selected_pizza):
print("Here are your selected pizzas")
total_price = 0
for selected in selected_pizza:
print("%s: $%s" % pizzas_with_prices[selected])
total_price = total_price + pizzas_with_prices[selected][1]
print("Here is the total price of pizzas:${}".format(total_price))
pizzaAmount = amountFunction()
pizzas_with_prices = [("BBQ Meatlovers", 8.5), ("Chicken Supreme", 8.5), ("Peri-Peri Chicken", 8.5),
("Vegorama", 8.5), ("Cheesy Bacon Hawaiian", 8.5), ("Beef and Onion", 8.5),
("Simply Cheese", 8.5), ("Ham and Cheese", 13.5), ("Hawaiian", 13.5),
("Veg Trio", 13.5), ("Peperoni", 13.5), ("Wedge", 13.5)]
selected_pizza = selectManyPizzas(pizzas_with_prices,pizzaAmount)
showSlectedPizzas(pizzas_with_prices, selected_pizza)
希望对您有所帮助。
抱歉,如果之前有人问过这个问题,但我在其他问题中找不到确切的答案或足够接近的答案。我也是 Python 的新手。
我正在寻找一种方法来检查从元组中选择比萨饼时用户输入是否有效。 (例如,如果用户输入任何超过 11 的值,我希望程序重新启动该功能。)
到目前为止,这是我的程序的完整代码。
def amountFunction():
global pizzaAmount
pizzaAmount = []
while pizzaAmount is not int:
try:
pizzaAmount = int(input("Please input the amount of pizzas you would like - (a maximum of 5)"))
except ValueError:
print("Please input a value above 0 or less than 5")
except pizzaAmount ==0 or pizzaAmount > 5:
print("Please input a value above 0 or less than 5")
amountFunction()
else:
print("Your amount of pizzas is " + str(pizzaAmount))
break
amountFunction()
def selectFunction():
global pizzas_with_prices
pizzas_with_prices = [("BBQ Meatlovers", 8.5), ("Chicken Supreme", 8.5), ("Peri-Peri Chicken", 8.5),
("Vegorama", 8.5), ("Cheesy Bacon Hawaiian", 8.5), ("Beef and Onion", 8.5),
("Simply Cheese", 8.5), ("Ham and Cheese", 13.5), ("Hawaiian", 13.5),
("Veg Trio", 13.5), ("Peperoni", 13.5), ("Wedge", 13.5)]
for index, pizza in enumerate(pizzas_with_prices):
print("%d %s: $%s" % (index, pizza[0], pizza[1]))
global selected_pizza
selected_pizza=[]
for n in range(pizzaAmount):
while selected_pizza is not int:
try:
selected_pizza = selected_pizza + [int(input("Choose a pizza: "))]
except ValueError:
print("Please select a pizza on the list")
else:
break
global total_price
total_price = 0
for selected in selected_pizza:
total_price += pizzas_with_prices[selected][1]
selectFunction()
def totalFunction():
print("Here are your selected pizzas")
for selected in selected_pizza:
print("%s: $%s" % pizzas_with_prices[selected])
print("Here is the total price of pizzas:${}".format(total_price))
totalFunction()
有问题的函数是下面这个 - def selectFunction():
def selectFunction():
global pizzas_with_prices
pizzas_with_prices = [("BBQ Meatlovers", 8.5), ("Chicken Supreme", 8.5), ("Peri-Peri Chicken", 8.5),
("Vegorama", 8.5), ("Cheesy Bacon Hawaiian", 8.5), ("Beef and Onion", 8.5),
("Simply Cheese", 8.5), ("Ham and Cheese", 13.5), ("Hawaiian", 13.5),
("Veg Trio", 13.5), ("Peperoni", 13.5), ("Wedge", 13.5)]
for index, pizza in enumerate(pizzas_with_prices):
print("%d %s: $%s" % (index, pizza[0], pizza[1]))
global selected_pizza
selected_pizza=[]
for n in range(pizzaAmount):
while selected_pizza is not int:
try:
selected_pizza = selected_pizza + [int(input("Choose a pizza: "))]
except ValueError:
print("Please select a pizza on the list")
else:
break
global total_price
total_price = 0
for selected in selected_pizza:
total_price += pizzas_with_prices[selected][1]
selectFunction()
我将如何检查用户输入是否在 list/tuple 比萨饼上? (例如,如果用户为 selected_pizza 输入 999,我希望程序再次重复 selectFunction() 直到选择了有效的比萨饼,然后继续计算比萨饼的总价格,然后继续下一个函数 totalFunction() )
我试过使用 IndexError: 但我似乎不明白如何使用它,因为它似乎不起作用,要么跳过并给我一个错误,要么进入无限循环。本质上我需要一种处理索引错误的方法。
以下是目前发生的情况的示例。
Please input the amount of pizzas you would like - (a maximum of 5)2
Your amount of pizzas is 2
0 BBQ Meatlovers: .5
1 Chicken Supreme: .5
2 Peri-Peri Chicken: .5
3 Vegorama: .5
4 Cheesy Bacon Hawaiian: .5
5 Beef and Onion: .5
6 Simply Cheese: .5
7 Ham and Cheese: .5
8 Hawaiian: .5
9 Veg Trio: .5
10 Pepperoni: .5
11 Wedge: .5
Choose a pizza: 23456
Choose a pizza: 4235464
Traceback (most recent call last):
File "C:\Users\ilyas rosslan\Documents\Python Work\tester.py", line 45, in
<module>
selectFunction()
File "C:\Users\ilyas rosslan\Documents\Python Work\tester.py", line 42, in
selectFunction
total_price += pizzas_with_prices[selected][1]
IndexError: list index out of range
如果有人能提供帮助,我们将不胜感激。谢谢
伊利亚斯。
读取输入,验证它,然后将它添加到 selected_pizza
列表。
for n in range(pizzaAmount):
while True:
try:
selection = int(input("Choose a pizza: "))
if selection in range(len(pizza_with_prices)):
selected_pizza.append(selection)
break
else:
print("Please select one of the listed pizza numbers")
except ValueError:
print("Please select a pizza on the list")
while selected_pizza is not int:
不正确。该条件将始终为真,因为 selected_pizza
是一个列表,而列表不是 int
.
你也应该停止使用这么多全局变量。 amountFunction()
应该 return 金额,然后它应该作为参数传递给 selectFunction()
。
据我了解,我会这样做。我为 select 一个有效的比萨创建了一个单独的函数,然后添加到 select 许多比萨的函数。
def amountFunction():
try:
pizzaAmount = int(input("Please input the amount of pizzas you would like - (a maximum of 5)"))
if pizzaAmount > 0 and pizzaAmount <=5:
return pizzaAmount
else:
print("Please input a value above 0 or less than 5")
return amountFunction()
except ValueError:
return amountFunction()
except KeyboardInterrupt:
print("user aborted the program")
return 0
def selectOnePizza(pizzas_with_prices):
try:
selected_pizza = int(input("Choose a pizza: "))
if selected_pizza >= 0 and selected_pizza <len(pizzas_with_prices):
return selected_pizza
else:
print("Please input a value above {} or less than {}".format(0,len(pizzas_with_prices)))
return selectOnePizza(pizzas_with_prices)
except ValueError:
print("Please input a value above {} or less than {}".format(0,len(selected_pizza)))
return selectOnePizza(pizzas_with_prices)
except KeyboardInterrupt:
print("user aborted the program")
return -1
def selectManyPizzas(pizzas_with_prices, pizzaAmount):
for index, pizza in enumerate(pizzas_with_prices):
print("%d %s: $%s" % (index, pizza[0], pizza[1]))
selected_pizza=[]
for n in range(pizzaAmount):
selected_pizza.append(selectOnePizza(pizzas_with_prices))
return selected_pizza
def showSlectedPizzas(pizzas_with_prices, selected_pizza):
print("Here are your selected pizzas")
total_price = 0
for selected in selected_pizza:
print("%s: $%s" % pizzas_with_prices[selected])
total_price = total_price + pizzas_with_prices[selected][1]
print("Here is the total price of pizzas:${}".format(total_price))
pizzaAmount = amountFunction()
pizzas_with_prices = [("BBQ Meatlovers", 8.5), ("Chicken Supreme", 8.5), ("Peri-Peri Chicken", 8.5),
("Vegorama", 8.5), ("Cheesy Bacon Hawaiian", 8.5), ("Beef and Onion", 8.5),
("Simply Cheese", 8.5), ("Ham and Cheese", 13.5), ("Hawaiian", 13.5),
("Veg Trio", 13.5), ("Peperoni", 13.5), ("Wedge", 13.5)]
selected_pizza = selectManyPizzas(pizzas_with_prices,pizzaAmount)
showSlectedPizzas(pizzas_with_prices, selected_pizza)
希望对您有所帮助。