最近,我开始学习Python。我想进行余弦分布模拟。 "索引错误:列表索引超出范围"显示在我的显示屏上。但我想我定义了一个列表的长度,我写了0<关键< LEN(厚度)。为什么错误出现在我的显示器上?
import math
def main():
height = 20
thickness = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
with open('output/output.csv', 'w', encoding = 'utf-8' ) as f:
f.write('x,thikcness\n')
for t in range(90):
deg = t
rad = math.radians(deg)
x = height * math.tan(rad)
key = round(x)
if 0 < key <= len(thickness):
thickness[key] += 1
for t in len(thickness):
f.write(str(thickness[t]) + '\n')
if __name__ == "__main__":
main()
答案 0 :(得分:0)
应该是make html
。这是因为数组索引从if 0 <= key < len(thickness):
到0
,而不是len(thickness)-1
到1
。