【发布时间】:2020-07-03 10:23:39
【问题描述】:
我试图错误处理 donuts_gone 的输入。用户应输入列表的元素位置。例如,如果他们想删除 18 美元的 6 个奶油甜甜圈的订单,那么您应该输入列表的位置;首先列出位置0,然后删除2d列表中的列表元素位置。我的程序的问题是,如果输入的数字不在列表中可用元素的范围内,我不知道如何编写 elif 语句:
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? ")
谢谢!我希望这是有道理的! :)
【问题讨论】:
标签: python list function error-handling