因此,我试图做一个简单的循环,由于某种原因,我似乎无法理解为什么出现错误消息。
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
开始的迭代计数器:
tax = []
for i, earning in enumerate(earnings, 1):
if earning <= 23000:
break
tax.append(0.14 * earning)
print('Tax calculation has been completed')
print('Number of iterations: ', i)
关于python - 在Python的while循环中追加列表会出现错误消息“列表索引超出范围”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58359157/