【问题标题】:is it possible to print everything in total instead of printing the percentage of every action是否可以打印所有内容而不是打印每个动作的百分比
【发布时间】:2019-10-24 09:42:20
【问题描述】:

我尝试编写一个对于初学者来说非常简单的投币模拟器,但随后我尝试实现一个百分比系统,该系统可以计算正面或反面翻转的百分比。那时我注意到我被困住了,我可能需要一些帮助。

我尝试打破 for 循环并排除从 x 变量到打印的所有内容

import random


heads = "Heads"
tails = "Tails"
flips = int(input("How many flips do you want to toss?: "))
print("................................")
cointoss = [(heads), (tails)]


for flips in range(flips):
    toss = random.choice(cointoss)
    print(toss)
    X = toss.count(heads)
    Y = toss.count(tails)
    Z = 100/(int(X)+int(Y))
    print('Percent of X: '+str(X*Z)+'%')
    print('Percent of Y: '+str(Y*Z)+'%')

这是结果



How many flips do you want to toss?: 4
................................
Heads
Percent of X: 100.0%
Percent of Y: 0.0%
Heads
Percent of X: 100.0%
Percent of Y: 0.0%
Heads
Percent of X: 100.0%
Percent of Y: 0.0%
Tails
Percent of X: 0.0%
Percent of Y: 100.0%

我希望代码显示整个循环的总百分比,而不是根据结果显示正面或反面的 100%。

【问题讨论】:

    标签: python-3.x for-loop random range


    【解决方案1】:

    试试这个;

    import random
    
    Headcount=0
    Tailcount=0
    heads = "Heads"
    tails = "Tails"
    flips = int(input("How many flips do you want to toss?: "))
    print("................................")
    cointoss = [(heads), (tails)]
    
    
    for flips in range(flips):
        toss = random.choice(cointoss)
        If toss == heads:
            Headcount+=1
        else:
            Tailcount+=1
        print(toss)
        X = Headcount
        Y = Tailcout
        print('Percent of X: '+str((X/X+Y)*100)+'%')
        print('Percent of Y: '+str((Y/Y+X)*100)+'%')
    

    【讨论】:

      【解决方案2】:

      首先,您没有正确计算百分比,因为您在进行时没有保持对当前旋转次数的计数。您可以做的是使用当前的自旋指数来计算其中的百分比:

      percentageOfHeads = currentNumberOfHeads / currentNumberOfFlips * 100

      percentageOfTails = currentNumberOfTails / currentNumberOfFlips * 100

      import random
      
      heads = "Heads"
      tails = "Tails"
      numHeads = 0
      numTails = 0
      flips = int(input("How many flips do you want to toss?: "))
      print("................................")
      cointoss = [heads, tails]
      
      
      for flip in range(flips):
          toss = random.choice(cointoss)
      
          if toss == heads:
              numHeads += 1
          else:
              numTails += 1
      
          print("Flip #" + str(flip + 1) + ": " + toss)
      
          percentHeads = numHeads / (flip + 1) * 100
          percentTails = numTails / (flip + 1) * 100
      
          print('Percent of Heads: ' + str(percentHeads) + '%')
          print('Percent of Tails: ' + str(percentTails) + '%')
      

      【讨论】:

      • 这让我跳出框框思考,因为我真的被卡住了。谢谢!
      【解决方案3】:

      您需要一些额外的变量来计算到目前为止正面和反面的总数,以及目前为止完成的翻转次数。以现在的方式,您没有任何关于到目前为止看到的正面/反面总数的信息,只有当前翻转的结果,因此您无法准确计算百分比。

      我建议在循环外添加变量,以获取正面总数和反面总数。每当您进行翻转时,您都会增加相应的计数器,然后您可以根据这些计数器计算整体正面/反面的百分比。

      【讨论】:

        猜你喜欢
        • 2019-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        相关资源
        最近更新 更多