<p>目标是将python代码编写到:</p>
<p>(1)模拟10000次抛硬币,并在列表中记录正面(H)和反面(T)值。我在下面的代码中将其命名为expList。
(2) 计算6个头部或6个尾部连续出现的条纹数,然后计算条纹占总翻转的百分比</p>
<p><strong>以下代码中是否有错误:</strong></p>
<pre><code>import random
numberOfStreaks = 0
expList = []
for expNumber in range(10000):
if random.randint(0,1)==0:
expList.append('H')
else:
expList.append('T')
for i in range(len(expList)-5):
if expList[i] == 'T' and expList[(i+1)]=='T' and expList[(i+2)]=='T' and expList[(i+3)]=='T' and expList[(i+4)]=='T' and expList[(i+5)]=='T':
numberOfStreaks+=1
elif expList[i] == 'H' and expList[(i+1)]=='H' and expList[(i+2)]=='H' and expList[(i+3)]=='H' and expList[(i+4)]=='H' and expList[(i+5)]=='H':
numberOfStreaks+=1
print(numberOfStreaks)
print(f'Chances of streak : {numberOfStreaks*100/10000}')
</code></pre>
<p>当我尝试不同的翻转次数(比如100000或1000次,而不是10000次)时,我得到了各种各样的概率百分比</p>