标签云

微信群

扫码加入我们

WeChat QR Code

Recently, I have started learning Python. I'd like to make a cosine distribution simulation."Index Error:list index out of range" is shown on my display. but I think I define a length of list, and I write0 < key < len(thickness). why the Error is shown on my display??import mathdef 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 = trad = math.radians(deg)x = height * math.tan(rad)key = round(x)if 0 < key <= len(thickness):thickness[key] += 1for t in len(thickness):f.write(str(thickness[t]) + '\n')if __name__ == "__main__":main()


Because you wrote key <= len(thickness) instead of key < len(thickness)

2019年07月24日41分02秒

To elaborate, the indices of thickness begin at 0 in python, not 1.

2019年07月24日41分02秒