我的错误消息是
文件“/Users/CoconutTime/Downloads/sista_bomben.py”,第60行,以概率统计2表示 百分比_2=(计数[j]/频率)*100 索引器:列表索引超出范围
我真的不明白如何改变它,或者它是怎么错的。 它可以很好地计算出1个骰子的概率,当它计算出超过1个骰子时,它就错了
对于10次投掷,它表明2个骰子(2,3,4,5…12)之和的总概率为零。不可能是这样的
#Programmet ska beräkna den relativa frekvensen för de olika utfallen (tärningssummorna) för 2 till 6 tärningar
import random
#tom lista för x antal tärningar. När vi skriver rolled.append()
rolled1 = []
rolled2 = []
rolled3 = []
rolled4 = []
rolled5 = []
rolled6 = []
#Hur många gånger 1-6 tärningar kastats
kastat1 = 0
kastat2 = 0
kastat3 = 0
kastat4 = 0
kastat5 = 0
kastat6 = 0
#Hur många gånger vi kastar tärningarna
freq = 10
def kast1():
# Den random summa av 1 tärning
dice1 = random.randrange(1,7)
return dice1
def kast2():
# Den random summa av 2 tärningar
dice2 = random.randrange(1,7)+ random.randrange(1,7)
return dice2
def kast3():
# Den random summa av 3 tärningar
dice3 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
return dice3
def kast4():
# Den random summa av 4 tärningar
dice4 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
return dice4
def kast5():
# Den random summa av 5 tärningar
dice5 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
return dice5
def kast6():
# Den random summa av 6 tärningar
dice6 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
return dice6
def probability_sums():
# För i mellan 1-6, räkna hur många gånger 1-6 inträffar / antal kast och * hundra för att få det i procent
for i in range(0,6):
percentage = (count[i] / freq)*100
# Vi vill ha vårt procent i string (text)
procent = str(percentage)
print('Probability for 1 dice:', i + 1, ':', procent)
for j in range(1,12):
# För j mellan 2-12, räkna hur många gånger 2,3,4...12 inträffar / antal kast och * hundra för att få det i procent
percentage_2 = (count[j] / freq)*100
procent_2 = str(percentage_2)
print('Probability for 2 dice:', j + 1, ':', procent_2)
for k in range(2,18):
percentage_3 = (count[k] / freq)*100
procent_3 = str(percentage_3)
print('Probability for 3 dice:', k + 1, ':', procent_3)
for l in range(3,24):
percentage_4 = (count[l] / freq)*100
procent_4 = str(percentage_4)
print('Probability for 4 dice:', l + 1, ':', procent_4)
for m in range(4,30):
percentage_5 = (count[m] / freq)*100
procent_5 = str(percentage_5)
print('Probability for 5 dice:', m + 1, ':', procent_5)
for n in range(5,36):
percentage_6 = (count[n] / freq)*100
procent_6 = str(percentage_6)
print('Probability for 6 dice:', n + 1, ':', procent_6)
def summa_dice2():
global rolled2
dice2 = random.randrange(1,7)+ random.randrange(1,7)
for i in range(2,13):
if dice2 == i:
rolled2.append(dice2)
return rolled2
def summa_dice3():
global rolled3
dice3 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
for i in range(3,19):
if dice3 == i:
rolled3.append(dice3)
return rolled3
def summa_dice4():
global rolled4
dice4 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
for j in range(4,25):
if dice4 == j:
rolled4.append(dice4)
return rolled4
def summa_dice5():
global rolled5
dice5 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
for k in range(5,31):
if dice5 == k:
rolled5.append(dice5)
return rolled5
def summa_dice6():
global rolled6
dice6 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
for l in range(6,37):
if dice6 == l:
rolled6.append(dice6)
return rolled6
for i in range(1,freq + 1):
antal_kast1 = kast1()
rolled1.append(antal_kast1)
kastat1 += 1
antal_kast2 = kast2()
rolled1.append(antal_kast2)
kastat2 += 1
antal_kast3 = kast3()
rolled1.append(antal_kast3)
kastat3 += 1
antal_kast4 = kast4()
rolled1.append(antal_kast4)
kastat4 += 1
antal_kast5 = kast5()
rolled1.append(antal_kast5)
kastat5 += 1
antal_kast6 = kast6()
rolled1.append(antal_kast6)
kastat6 += 1
#count räknar hur många gånger 1,2,3,4,5 och 6 uppkommer i listan. Om listan ser ut som list=[1,2,1,1,3] så skulle list.count(1) visa 3 eftersom det finns 3 ettor i listan.
count = [rolled1.count(1),rolled1.count(2),rolled1.count(3),rolled1.count(4),rolled1.count(5),rolled1.count(6)]
print('FOR 1 DICE:\nAfter being rolled',freq,' times:\n\n1 is rolled ',rolled1.count(1),'times\n2 is rolled ',rolled1.count(2),'times\n3 is rolled ',rolled1.count(3),'times\n4 is rolled ',rolled1.count(4),'times\n5 is rolled',rolled1.count(5),'times\n6 is rolled',rolled1.count(6),' times\n')
count2 = [rolled2.count(2),rolled2.count(3),rolled2.count(4),rolled2.count(5),rolled2.count(6),rolled2.count(7),rolled2.count(8),rolled2.count(9),rolled2.count(10),rolled2.count(11),rolled2.count(12)]
print('FOR 2 DICES:\nAfter being rolled',freq,' times:\n2 is rolled ',rolled2.count(2),'times\n3 is rolled ',rolled2.count(3),'times\n4 is rolled ',rolled2.count(4),'times\n5 is rolled',rolled2.count(5),'times\n6 is rolled',rolled2.count(6),'times\n7 is rolled',rolled2.count(7),'times\n8 is rolled',rolled2.count(8),'\n9 is rolled',rolled2.count(9),'times\n10 is rolled',rolled2.count(10),'\n11 is rolled',rolled2.count(11),'times\n12 is rolled',rolled2.count(12),' times\n')
count3 = [rolled3.count(3),rolled3.count(4),rolled3.count(5),rolled3.count(6),rolled3.count(7),rolled3.count(8),rolled3.count(9),rolled3.count(10),rolled3.count(11),rolled3.count(12),rolled3.count(13),rolled3.count(14),rolled3.count(15),rolled3.count(16),rolled3.count(17),rolled3.count(18)]
print('FOR 3 DICES:\nAfter being rolled',freq,' times:\n3 is rolled ',rolled3.count(3),'times\n4 is rolled ',rolled3.count(4),'times\n5 is rolled',rolled3.count(5),'times\n6 is rolled',rolled3.count(6),'times\n7 is rolled',rolled3.count(7),'times\n8 is rolled',rolled3.count(8),'times\n9 is rolled',rolled3.count(9),'times\n10 is rolled',rolled3.count(10),'times\n11 is rolled',rolled3.count(11),'times\n12 is rolled',rolled3.count(12),'times\n13 is rolled',rolled3.count(13),'times\n14 is rolled',rolled3.count(14),'times\n15 is rolled',rolled3.count(15),'times\n16 is rolled',rolled3.count(16),'times\n17 is rolled',rolled3.count(17),'times\n18 is rolled',rolled3.count(18),' times\n')
count4 = [rolled4.count(4),rolled4.count(5),rolled4.count(6),rolled4.count(7),rolled4.count(8),rolled4.count(9),rolled4.count(10),rolled4.count(11),rolled4.count(12),rolled4.count(13),rolled4.count(14),rolled4.count(15),rolled4.count(16),rolled4.count(17),rolled4.count(18),rolled4.count(19),rolled4.count(20),rolled4.count(21),rolled4.count(22),rolled4.count(23),rolled4.count(24)]
print('FOR 4 DICES:\nAfter being rolled',freq,' times\n4 is rolled ',rolled4.count(4),'times\n5 is rolled',rolled4.count(5),'times\n6 is rolled',rolled4.count(6),'times\n7 is rolled',rolled4.count(7),'times\n8 is rolled',rolled4.count(8),'times\n9 is rolled',rolled4.count(9),'times\n10 is rolled',rolled4.count(10),'times\n11 is rolled',rolled4.count(11),'times\n12 is rolled',rolled4.count(12),'times\n13 is rolled',rolled4.count(13),'times\n14 is rolled',rolled4.count(14),'times\n15 is rolled',rolled4.count(15),'times\n16 is rolled',rolled4.count(16),'times\n17 is rolled',rolled4.count(17),'times\n18 is rolled',rolled4.count(18),'times\n18 is rolled',rolled4.count(18),'times\n19 is rolled',rolled4.count(19),'times\n20 is rolled',rolled4.count(20),'times\n22 is rolled',rolled4.count(22),'times\n23 is rolled',rolled4.count(23),'times\n24 is rolled',rolled4.count(24),' times\n')
count5 = [rolled5.count(5),rolled5.count(6),rolled5.count(7),rolled5.count(8),rolled5.count(9),rolled5.count(10),rolled5.count(11),rolled5.count(12),rolled5.count(13),rolled5.count(14),rolled5.count(15),rolled5.count(16),rolled5.count(17),rolled5.count(18),rolled5.count(19),rolled5.count(20),rolled5.count(21),rolled5.count(22),rolled5.count(23),rolled5.count(24),rolled5.count(25),rolled5.count(26),rolled5.count(27),rolled5.count(28),rolled5.count(29),rolled5.count(30)]
print('FOR 5 DICES:\nAfter being rolled',freq,' times\n5 is rolled',rolled5.count(5),'times\n6 is rolled',rolled5.count(6),'times\n7 is rolled',rolled5.count(7),'times\n8 is rolled',rolled5.count(8),'times\n9 is rolled',rolled5.count(9),'times\n10 is rolled',rolled5.count(10),'times\n11 is rolled',rolled5.count(11),'times\n12 is rolled',rolled5.count(12),'times\n13 is rolled',rolled5.count(13),'times\n14 is rolled',rolled5.count(14),'times\n15 is rolled',rolled5.count(15),'times\n16 is rolled',rolled5.count(16),'times\n17 is rolled',rolled5.count(17),'times\n18 is rolled',rolled5.count(18),'times\n18 is rolled',rolled5.count(18),'times\n19 is rolled',rolled5.count(19),'times\n20 is rolled',rolled5.count(20),'times\n22 is rolled',rolled5.count(22),'times\n23 is rolled',rolled5.count(23),'times\n24 is rolled',rolled5.count(24),'times\n25 is rolled',rolled5.count(25),'times\n26 is rolled',rolled5.count(26),'times\n27 is rolled',rolled5.count(27),'times\n28 is rolled',rolled5.count(28),'times\n29 is rolled',rolled5.count(29),'times\n30 is rolled',rolled5.count(30),' times\n')
count6 = [rolled6.count(6),rolled6.count(7),rolled6.count(8),rolled6.count(9),rolled6.count(10),rolled6.count(11),rolled6.count(12),rolled6.count(13),rolled6.count(14),rolled6.count(15),rolled6.count(16),rolled6.count(17),rolled6.count(18),rolled6.count(19),rolled6.count(20),rolled6.count(21),rolled6.count(22),rolled6.count(23),rolled6.count(24),rolled6.count(25),rolled6.count(26),rolled6.count(27),rolled6.count(28),rolled6.count(29),rolled6.count(30)]
print('FOR 6 DICES:\nAfter being rolled',freq,'times\n6 is rolled',rolled6.count(6),'times\n7 is rolled',rolled6.count(7),'times\n8 is rolled',rolled6.count(8),'times\n9 is rolled',rolled6.count(9),'times\n10 is rolled',rolled6.count(10),'times\n11 is rolled',rolled6.count(11),'times\n12 is rolled',rolled6.count(12),'times\n13 is rolled',rolled6.count(13),'times\n14 is rolled',rolled6.count(14),'times\n15 is rolled',rolled6.count(15),'times\n16 is rolled',rolled6.count(16),'times\n17 is rolled',rolled6.count(17),'times\n18 is rolled',rolled6.count(18),'times\n18 is rolled',rolled6.count(18),'times\n19 is rolled',rolled6.count(19),'times\n20 is rolled',rolled6.count(20),'times\n22 is rolled',rolled6.count(22),'times\n23 is rolled',rolled6.count(23),'times\n24 is rolled',rolled6.count(24),'times\n25 is rolled',rolled6.count(25),'times\n26 is rolled',rolled6.count(26),'times\n27 is rolled',rolled6.count(27),'times\n28 is rolled',rolled6.count(28),'times\n29 is rolled',rolled6.count(29),'times\n30 is rolled',rolled6.count(30),'times\n31 is rolled',rolled6.count(31),'times\n32 is rolled',rolled6.count(32),'times\n33 is rolled',rolled6.count(33),'times\n34 is rolled',rolled6.count(34),'times\n35 is rolled',rolled6.count(35),'times\n36 is rolled',rolled6.count(36),' times\n')
probability_sums()
summa_dice2()
summa_dice3()
summa_dice4()
summa_dice5()
summa_dice6()
这里有一个不计算百分比的部分解,但可以扩展以给出该解
印刷品:
randrange(1, 6)
将生成包含1到5的整数,而不是6。您应该使用randint(1, 6)
从文档:
除非你有5张脸骰子,否则你可能会想做randrange(1,7)
相关问题 更多 >
编程相关推荐