Python3列表索引错误

时间:2018-01-25 03:07:21

标签: python python-3.x list

最近,我开始学习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()

1 个答案:

答案 0 :(得分:0)

应该是make html。这是因为数组索引从if 0 <= key < len(thickness):0,而不是len(thickness)-11