【问题标题】:How do I print out each random result of the coin toss and how to print the best streak of a certain side? (Python)如何打印出抛硬币的每个随机结果以及如何打印出某一侧的最佳连胜? (Python)
【发布时间】:2020-08-01 18:15:02
【问题描述】:

我必须创建一个模拟抛硬币的程序。用户将能够输入投掷硬币的次数。然后程序将随机“扔”它并打印出结果 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: ",)

如果有人可以帮助我,那就太好了!

【问题讨论】:

    标签: python


    【解决方案1】:

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

    正确的解决方案 -

    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
    

    你哪里出错了 -

    你的代码 -

    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
    

    您的代码将陷入无限循环,因为循环永远不会跳出循环。您正在增加投掷次数以及头/尾数。您只需要增加头/尾数而不是抛掷数。可以根据我上面的解决方案进行其他更改。

    【讨论】:

    • 你知道它如何打印结果 1、结果 2、结果 3 等的折腾结果吗?我无法将数字累积到结果部分,所以它只是为所有结果显示结果 1
    • 您可以将 print 语句放在 while 循环本身中。除此之外,请保留一个计数变量,因此您可以这样做 - print('result '+str(count)+' '+str(coin))。将此语句放在硬币之后
    • 我想知道为什么有toss = heads + tails然后在下一行toss = int(input("How many coin tosses would you like to simulate?"))
    【解决方案2】:

    另一种可能性是使用字典(不防止用户输入不可转换为 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"]}')
                                                       
    

    【讨论】:

      猜你喜欢
      • 2022-10-30
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多