英文:
I am working on a python practice problem, that involves calculating the probability of consecutive coin flips. I can't tell why percentages over 100%
问题
I have reviewed your Python code for the "Coin flip streaks" problem. It seems that there are some HTML escape sequences in your code that need to be corrected. Here's the corrected code:
import random
numberOfStreaks = 0
for experimentNumber in range(10000):
# Code that creates a list of 100 'heads' or 'tails' values.
coinFlips = []
for i in range(100):
if random.randint(0, 1) == 0:
coinFlips.append('H')
else:
coinFlips.append('T')
# Check if there is a streak of 6 heads or tails in a row
numberOfHeads = 0
numberOfTails = 0
for i in coinFlips:
if i == 'H':
numberOfHeads += 1
numberOfTails = 0 # Reset the streak of tails
elif i == 'T':
numberOfTails += 1
numberOfHeads = 0 # Reset the streak of heads
if numberOfHeads == 6 or numberOfTails == 6:
numberOfStreaks += 1
numberOfHeads = 0 # Reset the streak of heads
numberOfTails = 0 # Reset the streak of tails
print('Chance of streak: %s%%' % (numberOfStreaks / 100))
The issue you mentioned, where you were getting a chance of a streak over 100%, should be resolved with this corrected code. If you have any further questions or encounter any issues, feel free to ask for assistance.
英文:
I am working on this practice problem from the book "Automate the Boring Stuff with Python", the second edition. Here is the exercise I am doing:
> #### Coin flip streaks
>
> For this exercise, we’ll try doing an experiment. If you flip a coin 100 times and write down an “H” for each heads and “T” for each tails, you’ll create a list that looks like “T T T T H H H H T T.” If you ask a human to make up 100 random coin flips, you’ll probably end up with alternating head-tail results like “H T H T H H T H T T,” which looks random (to humans), but isn’t mathematically random. A human will almost never write down a streak of six heads or six tails in a row, even though it is highly likely to happen in truly random coin flips. Humans are predictably bad at being random.
>
> Write a program to find out how often a streak of six heads or a streak of six tails comes up in a randomly generated list of heads and tails. Your program breaks up the experiment into two parts: the first part generates a list of randomly selected 'heads' and 'tails' values, and the second part checks if there is a streak in it. Put all of this code in a loop that repeats the experiment 10,000 times so we can find out what percentage of the coin flips contains a streak of six heads or tails in a row. As a hint, the function call random.randint(0, 1) will return a 0 value 50% of the time and a 1 value the other 50% of the time.
>
> You can start with the following template:
>
> python
> import random
> numberOfStreaks = 0
> for experimentNumber in range(10000):
> # Code that creates a list of 100 'heads' or 'tails' values.
>
> # Code that checks if there is a streak of 6 heads or tails in a row.
> print('Chance of streak: %s%%' % (numberOfStreaks / 100))
>
>
> Of course, this is only an estimate, but 10,000 is a decent sample size. Some knowledge of mathematics could give you the exact answer and save you the trouble of writing a program, but programmers are notoriously bad at math.
My attempt using the template code provided
import random
numberOfStreaks = 0
for experimentNUmber in range(10000):
# Create a list of 100 heats or tails values.
coinFlips = []
for i in range(100):
if random.randint(0, 1) == 0:
coinFlips.append('H')
else:
coinFlips.append('T')
# Check if there is a streak of 6 heads or tails in a row
numberOfHeads = 0
numberOfTails = 0
for i in coinFlips:
if i == 'H':
numberOfHeads += 1
numberOfTails = 0 # Reset the streak of tails
elif i == 'T':
numberOfTails += 1
numberOfHeads = 0 # Reset the streak of heads
if numberOfHeads == 6 or numberOfTails == 6:
numberOfStreaks += 1
numberOfHeads = 0 # Reset the streak of heads
numberOfTails = 0 # Reset the streak of tails
print('Chance of streak: %s%%' % (numberOfStreaks / 100))
When I run this code, I am getting outputs saying the change of a streak is over 100% (around the 150%-153% range). I have not been able to figure out why this is, I'm not sure where in my code is causing it to be this high, or if even it's a problem with my code at all, and it's actually the template code that's causing the problem. Or if I just misunderstood the question, because it took me a minute to really understand what it was asking. Any help would be appreciated. Thanks.
答案1
得分: 4
你在计算比所需更多的连续出现次数。在你的代码的 if numberOfHeads == 6 or numberOfTails == 6
部分的末尾,需要包含一个 break
,以处理出现6次连续头或尾的情况。
我添加了一张图片,因为Stackoverflow似乎不支持LaTeX。
以下是使用我定义的变量重写的代码。
import random
m = 100
k = 6
n = 10000
numberOfStreaks = 0
for experimentNUmber in range(n):
# 创建包含m个头或尾值的列表。
coinFlips = []
for i in range(m):
if random.randint(0, 1) == 0:
coinFlips.append('H')
else:
coinFlips.append('T')
# 检查是否有连续k个头或尾。
numberOfHeads = 0
numberOfTails = 0
for i in coinFlips:
if i == 'H':
numberOfHeads += 1
numberOfTails = 0 # 重置尾部连续
elif i == 'T':
numberOfTails += 1
numberOfHeads = 0 # 重置头部连续
if numberOfHeads == k or numberOfTails == k:
numberOfStreaks += 1
# 重要提示:出现k次连续的头或尾
break
print('连续出现的机会:%s%%' % ((numberOfStreaks / n)*100))
输出大约为80.00%(由于模拟中的随机性而在每次代码执行时更改)。
英文:
You're counting more streaks than needed. You need to include a break
at the end of the section if numberOfHeads == 6 or numberOfTails == 6
of your code, when a streak of 6 heads/tails comes up.
I'm adding an image because Stackoverflow doesn't seem to support latex.
Here's the code rewritten with the variables I've defined.
import random
m = 100
k = 6
n = 10000
numberOfStreaks = 0
for experimentNUmber in range(n):
# Create a list of m heats or tails values.
coinFlips = []
for i in range(m):
if random.randint(0, 1) == 0:
coinFlips.append('H')
else:
coinFlips.append('T')
# Check if there is a streak of k heads or tails in a row
numberOfHeads = 0
numberOfTails = 0
for i in coinFlips:
if i == 'H':
numberOfHeads += 1
numberOfTails = 0 # Reset the streak of tails
elif i == 'T':
numberOfTails += 1
numberOfHeads = 0 # Reset the streak of heads
if numberOfHeads == k or numberOfTails == k:
numberOfStreaks += 1
# IMPORTANT : A streak of k heads/tails comes up
break
print('Chance of streak: %s%%' % ((numberOfStreaks / n)*100))
The output is approximately 80.00% (it changes with each code execution due to randomness in the simulation).
答案2
得分: 1
我找到解决方法,只想用简单的话来说明。我只需要在出现连续的条件中添加一个中断。这就是错误的根源。
import random
numberOfStreaks = 0
for experimentNUmber in range(10000):
# 创建一个包含100个正面或反面值的列表。
coinFlips = []
for i in range(100):
if random.randint(0, 1) == 0:
coinFlips.append('H')
else:
coinFlips.append('T')
# 检查是否有6个连续的正面或反面。
numberOfHeads = 0
numberOfTails = 0
for i in coinFlips:
if i == 'H':
numberOfHeads += 1
numberOfTails = 0 # 重置反面的连续
elif i == 'T':
numberOfTails += 1
numberOfHeads = 0 # 重置正面的连续
if numberOfHeads == 6 or numberOfTails == 6:
numberOfStreaks += 1
numberOfHeads = 0 # 重置正面的连续
numberOfTails = 0 # 重置反面的连续
break # <== *在这里添加了这个*
print('连续的机会: %s%%' % (numberOfStreaks / 100))
这完全解决了问题,概率约为80%,与其他答案的代码一样。
英文:
I figured it out, and I just want to put the solution in simple terms. I just needed to add a break in the condition where a streak arises. That was the source of the error.
import random
numberOfStreaks = 0
for experimentNUmber in range(10000):
# Create a list of 100 heats or tails values.
coinFlips = []
for i in range(100):
if random.randint(0, 1) == 0:
coinFlips.append('H')
else:
coinFlips.append('T')
# Check if there is a streak of 6 heads or tails in a row
numberOfHeads = 0
numberOfTails = 0
for i in coinFlips:
if i == 'H':
numberOfHeads += 1
numberOfTails = 0 # Reset the streak of tails
elif i == 'T':
numberOfTails += 1
numberOfHeads = 0 # Reset the streak of heads
if numberOfHeads == 6 or numberOfTails == 6:
numberOfStreaks += 1
numberOfHeads = 0 # Reset the streak of heads
numberOfTails = 0 # Reset the streak of tails
break # <== *ADDED THIS RIGHT HERE*
print('Chance of streak: %s%%' % (numberOfStreaks / 100))
This completely resolved the issue and the probability sits around 80%, just as the other answer's code does.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论