【问题标题】:I can't figure out how to get total number from loops in flip coin game我不知道如何从抛硬币游戏的循环中获取总数
【发布时间】:2012-09-29 09:25:38
【问题描述】:

![抛硬币程序][1]

标题

我必须制作这个抛硬币程序,它会输出抛硬币的次数和正面或反面的数量。用户有机会尽可能多地玩,直到他们退出程序。我应该得到这个人翻转并得到头部或尾部的总次数。比如一个人玩了 3 次,第一次 10 次,第二次 15 次,第三次 20 次,那么总共 45 次。我无法让程序计算翻转、正面或反面的总数。同样在该人第一次玩之后,如果他们选择再次玩,他们不能输入低于之前数量的翻转次数。我不知道出了什么问题。

#coin toss

print "Welcome to my coin tossing game. I will flip a coin the amount"
print "of times you tell me to and show you your results."

import random
counter = 0
start = 0
user_input = 0
heads = 0
tails = 0

user_input = int(raw_input("Enter the number of times you want to flip the coin "))

while user_input > counter:
        chance = random.randrange(2)
        counter = counter + 1
        if chance == 1:
            heads = heads + 1
        else:
            tails = tails + 1

print "You flipped the coin", counter, "times."
print heads, "times came out heads"
print tails, "times came out tails"

headstotal = heads
tailstotal = tails

restart = raw_input("Would you like to try again? y or n ")
while restart == "y":
        user_input = int(raw_input("Enter the number of times you want to flip the coin"))
        while user_input > counter:
                chance = random.randrange(2)
                counter = counter + 1
                if chance == 1:
                        heads = heads + 1
                else:
                       tails = tails + 1
        print "You flipped the coin", counter, "times."
        print heads, "times came out heads."
        print tails, "times came out tails."

        restart = raw_input("Would you like to try again? y or n ")
        print "Thanks for playing."

【问题讨论】:

  • 你觉得哪里不对?它是否产生了您不期望的输出?
  • 您是否看到代码中的重复并想知道 - 没有更好的方法吗?

标签: python


【解决方案1】:

您需要在两次运行之间将计数器设置为 0

考虑重组代码以避免大量重复

while True:
    counter = 0
    user_input ...
        ...
    ...
    restart = raw_input("Would you like to try again? y or n ")
    if restart == "n":
        break
print "Thanks for playing."

【讨论】:

    【解决方案2】:

    您还可以使用标准的 collections.Counter 类: (http://docs.python.org/dev/library/collections#collections.Counter)

    c = Counter()
    c.update(["head"])
    

    这是一个使用常用python习语的版本

    #coin toss
    
    print "Welcome to my coin tossing game. I will flip a coin the amount"
    print "of times you tell me to and show you your results."
    
    import random, collections
    
    while True:
        counter = collections.Counter()    
        user_input = int(raw_input("Enter the number of times you want to flip the coin "))
    
        counter.update(random.choice("head", "tail") for i in range(user_input))
    
        print "You flipped the coin", user_input, "times."
        print counter["head"], "times came out heads"
        print counter["tail"], "times came out tails"
    
        restart = raw_input("Would you like to try again? y or n ")
        if restart != "y":
            break
    print "Thanks for playing."
    

    【讨论】:

      【解决方案3】:

      您的代码,已重构。

      #coin toss
      
      print "\t\tWelcome to my coin tossing game. I will flip a coin the amount"
      print "\t\tof times you tell me to and show you your results.\n"
      
      import random
      
      heads = 0
      tails = 0
      headstotal = 0
      tailstotal = 0
      
      while True:
          counter = user_input = int(raw_input("Enter the number of times you want to flip the coin "))
          while user_input > 0:
              chance = random.randrange(2)
              if chance==1:
                  heads+=1
                  headstotal+=1
              else:
                  tails+=1
                  tailstotal+=1
              user_input-=1
      
          print "You flipped the coin", counter, "times."
          print heads, "times came out heads"
          print tails, "times came out tails"
          heads=tails=0
          restart = raw_input("Would you like to try again? y or n")
          if restart=="n":
              break
      print "total heads = ",headstotal
      print "total tails = ",tailstotal   
      print "total flips = ", headstotal+tailstotal
      print "Thanks for playing."
      raw_input()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 2021-08-30
        • 1970-01-01
        • 2021-03-12
        • 1970-01-01
        • 2023-04-11
        相关资源
        最近更新 更多