所以我试着做一个简单的循环,出于某种原因,我似乎不明白为什么会出现错误消息
earnings = [94500,65377,84524]
deductions = [20000,18000,19000]
tax = [] #empty list
i = -1 #iterative counter
while True:
i=i+1
if (earnings[i] > 23000):
tax.append(0.14*earnings[i])
continue
else:
break
print ('Tax calculation has been completed')
print ('Number of iterations: ',i)
我觉得这跟线路有关
if (earnings[i] > 23000)
但我不知道该如何处理这个问题
您可以使用
enumerate
在earnings
列表上迭代,同时生成从1
开始的迭代计数器:在循环中没有检查索引是否超出范围的检查,即检查i与列表“收益”中的项数。请这样做:
相关问题 更多 >
编程相关推荐