如何打印抛硬币的每个随机结果以及如何打印某一面的最佳条纹?(Python)

2024-05-06 07:23:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须创建一个模拟抛硬币的程序。用户将能够输入硬币投掷的次数。然后,程序将随机“抛出”它,并打印结果1:尾部、结果2:头部等,直到输入值。我还需要在末尾打印尾部和头部的最高条纹。然而,我正在为这两个方面的代码而挣扎。不过我有随机抛硬币的密码

import random

heads = 0
tails = 0
toss = heads + tails

toss = int(input("How many coin tosses would you like to simulate?"))

while heads + tails < toss:
    coin = random.randint(1, 2)
    if coin == 1:
        toss += 1
        heads = heads + 1
    else:
        coin == 2
        toss += 1
        tails = tails + 1
print("The total amount of heads: ",heads)
print("the best streak of heads: ",)
print("The total amount of tails: ",tails)
print("The best streak of tails: ",)

如果有人能帮我,那就太好了


Tags: ofthe程序硬币randomamounttotalbest
2条回答

您可以通过以下方式实现您想要实现的目标-

正确的解决方案-

import random

heads = 0
tails = 0
toss = heads + tails

toss = int(input("How many coin tosses would you like to simulate?"))
max_head_streak,max_tail_streak,curr_head_streak,curr_tail_streak=0,0,0,0
while heads + tails < toss:
    coin = random.randint(1, 2)
    if coin == 1:
        # If coin is 1, we increase count of head
        heads = heads + 1

        # Since the current one is head, we just increase current head streak and reset the tail streak
        curr_head_streak += 1
        curr_tail_streak = 0

    else:
        # if coin isn't 1, we increase count of tail
        tails = tails + 1

        # Similarly, We just increase current tail streak and reset head streak in this case
        curr_tail_streak += 1
        curr_head_streak = 0
    
    # On each iteration, we set the max_head_streak and max_tail_streak as per below -
    max_head_streak=max(max_head_streak,curr_head_streak)
    max_tail_streak=max(max_tail_streak,curr_tail_streak)

print("The total amount of heads: ",heads)
print("The best streak of heads: ",max_head_streak)
print("The total amount of tails: ",tails)
print("The best streak of tails: ",max_tail_streak)

输出:

How many coin tosses would you like to simulate?10
The total amount of heads:  6
The best streak of heads:  3
The total amount of tails:  4
The best streak of tails:  2

哪里出了问题-

Your code -

while heads + tails < toss:
    coin = random.randint(1, 2)
    if coin == 1:
        toss += 1
        heads = heads + 1
    else:
        coin == 2
        toss += 1
        tails = tails + 1

您的代码将运行在一个无限循环中,因为循环永远不会脱离循环。您正在增加投掷计数以及头/尾计数。您只需要增加头/尾计数,而不需要增加投掷计数。其他更改可根据我的上述解决方案进行

另一种可能是使用dictionary(用户输入不可转换为int的值时无任何防御措施):

import random


tries = input('How many coin tosses you like to simulate: ')
tosses = (random.choice(['head', 'tail']) for i in range(int(tries)))
 
results = {'heads': {'total': 0, 'best': 0, 'current': 0},
          'tails': {'total': 0, 'best': 0, 'current': 0}
          }
 
# alternative: {key: dict.fromkeys('total best current'.split(), 0) for key in ('heads', 'tails')}

for i, toss in enumerate(tosses, start=1):
    print(f'Result {i}: {toss}') 
    results[toss]['total'] += 1
    results[toss]['current'] += 1
    results[(results.keys() - [toss]).pop()]['current'] = 0
    results[toss]['best'] = max(results[toss]['best'], results[toss]['current'])

for k, v in results.items():
    print(f'The total amount of {k} is {v["total"]}')
    print(f'The best streak of {k} is {v["best"]}')
                                                   

相关问题 更多 >