因此,我试图做一个简单的循环,由于某种原因,我似乎无法理解为什么出现错误消息。
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句