Python 如果二维列表的元素不在范围内,如何处理错误
我试图错误地处理油炸圈饼的输入。用户应输入列表的元素位置。例如,如果他们想以18美元的价格删除6个奶油甜甜圈的订单,那么你应该输入列表的位置;首先列出位置0,然后删除二维列表中的列表元素位置。我的程序的问题是,如果输入的数字不在列表中可用元素的范围内,我不知道如何编写elif语句:Python 如果二维列表的元素不在范围内,如何处理错误,python,list,function,error-handling,Python,List,Function,Error Handling,我试图错误地处理油炸圈饼的输入。用户应输入列表的元素位置。例如,如果他们想以18美元的价格删除6个奶油甜甜圈的订单,那么你应该输入列表的位置;首先列出位置0,然后删除二维列表中的列表元素位置。我的程序的问题是,如果输入的数字不在列表中可用元素的范围内,我不知道如何编写elif语句: elif i != total_order[i]: 简化程序: def check_valid(prompt): while True: try:
elif i != total_order[i]:
简化程序:
def check_valid(prompt):
while True:
try:
i = int(input(prompt))
if i == '':
print("You must enter a value for the doughnut item you would like to change.")
print()
elif i != total_order[i]:
print("Invalid")
else:
break
except:
break
return i
total_order = [['Cream',6,18], ['Cookies',5,20], ['Jam',6,16]]
for i in range(len(total_order)):
print("{} {} {} Doughnuts = ${:.2f}".format(i, total_order[i][1],total_order[i][0],total_order[i][2]))
doughnuts_gone = check_valid("Enter the number associated with the doughnut order you would like to remove? ")
谢谢大家!!我希望这是有道理的!:) 您将
i
变量作为字符串进行了检查(如果i='':
),但在前一行中它被强制转换为整数(i=int(输入(提示))
)。您应该检查列表的长度,而不是这一行:elif i!=订单总数[i]:
。由于此问题,您的脚本无法运行。我已经编写了一个工作代码,并对其进行了测试。请参阅下面我的代码/测试
此外,您的van还可以改进代码(例如:检查输入是否可以转换为整数)
代码:
def check_valid(prompt):
while True:
try:
i = input(prompt)
if not i:
print("You must enter a value for the doughnut item you would like to change.")
print()
elif int(i) > len(total_order)-1:
print("Invalid")
else:
break
except:
break
return int(i)
total_order = [['Cream', 6, 18], ['Cookies', 5, 20], ['Jam', 6, 16]]
for i in range(len(total_order)):
print("{} {} {} Doughnuts = ${:.2f}".format(i, total_order[i][1], total_order[i][0],
total_order[i][2]))
doughnuts_gone = check_valid(
"Enter the number associated with the doughnut order you would like to remove? ")
print("Valid value! Selected one: {}".format(total_order[doughnuts_gone]))
>>> python3 test.py
0 6 Cream Doughnuts = $18.00
1 5 Cookies Doughnuts = $20.00
2 6 Jam Doughnuts = $16.00
Enter the number associated with the doughnut order you would like to remove? 3
Invalid
Enter the number associated with the doughnut order you would like to remove?
You must enter a value for the doughnut item you would like to change.
Enter the number associated with the doughnut order you would like to remove? 0
Valid value! Selected one: ['Cream', 6, 18]
输出:
def check_valid(prompt):
while True:
try:
i = input(prompt)
if not i:
print("You must enter a value for the doughnut item you would like to change.")
print()
elif int(i) > len(total_order)-1:
print("Invalid")
else:
break
except:
break
return int(i)
total_order = [['Cream', 6, 18], ['Cookies', 5, 20], ['Jam', 6, 16]]
for i in range(len(total_order)):
print("{} {} {} Doughnuts = ${:.2f}".format(i, total_order[i][1], total_order[i][0],
total_order[i][2]))
doughnuts_gone = check_valid(
"Enter the number associated with the doughnut order you would like to remove? ")
print("Valid value! Selected one: {}".format(total_order[doughnuts_gone]))
>>> python3 test.py
0 6 Cream Doughnuts = $18.00
1 5 Cookies Doughnuts = $20.00
2 6 Jam Doughnuts = $16.00
Enter the number associated with the doughnut order you would like to remove? 3
Invalid
Enter the number associated with the doughnut order you would like to remove?
You must enter a value for the doughnut item you would like to change.
Enter the number associated with the doughnut order you would like to remove? 0
Valid value! Selected one: ['Cream', 6, 18]