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)
基本上你错了的是你的循环是从}是从count[11]上没有价值,这就是为什么有一个
1
开始的,你的count
{0
开始的,所以当你计算percentage_2
时,它是从index 1
开始的,例如:percentage_2 = (count[1] / freq)*100
,它跳过了你的0th index
,当你到达j=11
时,就有一个index range
index out of range error
相关问题 更多 >
编程相关推荐