我不知道列表索引如何超出范围

问题描述 投票:0回答:1

j达到5时停止计数,尽管我的范围以11结尾。

为什么会发生,我该如何解决?

我的部分代码有此问题:

dice1 = random.randrange(1,7)

def probabilities_sum():
    print('\nprobability for 2 dices:')
    for j in range(1, 12):
        percentage_2 = (count[j] / freq) * 100
        procent_2 = str(percentage_2)
        print('this is J', j)
        print(j + 1, ':', procent_2)
python list indexoutofboundsexception
1个回答
0
投票

尝试遍历之前尝试打印出计数内容。它可能在该索引处没有数据。

© www.soinside.com 2019 - 2024. All rights reserved.