【问题标题】:Dice game not working with multiple players骰子游戏不能与多个玩家一起使用
【发布时间】:2018-12-18 18:13:33
【问题描述】:

我正在尝试创建一个游戏,您可以在其中选择有多少玩家,命名这些玩家,然后随机选择一名玩家。

从那里,被选中的玩家会在 1 到 10 之间选择一个他们不想登陆的数字。

然后,掷骰子,如果他们落在那个数字上,其他人都可以为他/她选择一个敢。如果没有,游戏只是随机选择另一个玩家,然后重新开始这个过程。

但问题是,程序没有通过询问您不想登陆哪个号码的部分。这很奇怪,因为它适用于一个人。

这是完整的代码,你正在寻找第 23 行:

# Pass Or Dare

from random import randint
firsttry = True
def playerpicked(list):
    listwords = len(list)
    numpicked = randint(0, listwords - 1)
    userpicked = list[numpicked]
    return userpicked

while firsttry:
    try:
        playercount = int(input('How many players will there be?\n'))
    except ValueError:
        print("That isn't a number. Please enter a number without decimals.")
    else:
        firsttry = False
        playernames = [input("Name of Player {}: ".format(i)) for i in range(1, playercount + 1)]
        while True:
            playerturn = playerpicked(playernames)
            print("The Player picked is:",playerturn)
            while True:
                try:
                    darenum = int(input(playerturn + ", which number do you NOT want to land on?\n"))
                except ValueError:
                    print("Please enter a number.")
                else:
                    if darenum > 10 or darenum < 1:
                        print("Please enter a number between 1 and 10.\n")
                    else:
                        break
                    print("Okay. Rolling the dice...")
                    numpick = randint(1, 10)
                    print("The number chosen is " + str(numpick) + ".")
                    if numpick == darenum:
                        print("Whoops! The number you chose was the one that we landed on! Everyone agree on a dare for " + playerturn + "!\n\n")
                        input("Press Enter once " + playerturn + " has done a dare...\n")
                    else:
                        print(playerturn + " has escaped! Moving on to the next person.\n\n") 

【问题讨论】:

    标签: python dice


    【解决方案1】:

    这里是对算法的快速回顾,问题是您在检查darenumber 是否在所需范围之间时使用的中断,导致再次循环并且永远不会离开那里。 此外,建议您检查您的控制结构,因为它是造成问题的原因。 最后,我强烈推荐你的游戏的退出功能,因为在某些时候必须结束。希望对您有所帮助:

    from random import randint
    
    def playerpicked(list):
        listwords = len(list)
        numpicked = randint(0, listwords - 1)
        userpicked = list[numpicked]
        return userpicked
    
    playercount = 0
    while playercount < 1:
        try:
            playercount = int(input('How many players will there be?\n'))
        except ValueError:
            print("That isn't a number. Please enter a number without decimals.")
    
    playernames = [input("Name of Player {}: ".format(i)) for i in range(1, playercount + 1)]
    print("Press 0 while it's your turn to finish the game")
    while True:
        playerturn = playerpicked(playernames)
        print("The Player picked is:",playerturn)
        darenum = -1
        while (darenum > 10 or darenum < 0):
            try:
                darenum = int(input(playerturn + ", which number do you NOT want to land on?\n"))
                if darenum > 10 or darenum < 1:
                    print("Please enter a number between 1 and 10.\n")
            except ValueError:
                print("Please enter a number.")
        if darenum == 0:
            break
        print("Okay. Rolling the dice...")
        numpick = randint(1, 10)
        print("The number chosen is " + str(numpick) + ".")
        if numpick == darenum:
            print("Whoops! The number you chose was the one that we landed on! Everyone agree on a dare for " + playerturn + "!\n\n")
            input("Press Enter once " + playerturn + " has done a dare...\n")
        else:
            print(playerturn + " has escaped! Moving on to the next person.\n\n") 
    
    print("Game Over")
    

    【讨论】:

      【解决方案2】:

      您的原始代码,带有错误的汉化(阅读代码的 cmets):

      # Pass Or Dare
      
      from random import randint
      firsttry = True
      def playerpicked(list):
          listwords = len(list)
          numpicked = randint(0, listwords - 1)
          userpicked = list[numpicked]
          return userpicked
      
      while firsttry:
          try:
              playercount = int(input('How many players will there be?\n'))
          except ValueError:
              print("That isn't a number. Please enter a number without decimals.")
          else:
              firsttry = False
              playernames = [input("Name of Player {}: ".format(i)) for i in range(1, playercount + 1)]
              while True:
                  playerturn = playerpicked(playernames)
                  print("The Player picked is:",playerturn)
                  while True:
                      try:
                          darenum = int(input(playerturn + ", which number do you NOT want to land on?\n"))
                      except ValueError:
                          print("Please enter a number.")
                      else: #there is no if for this else
                          if darenum > 10 or darenum < 1:
                              print("Please enter a number between 1 and 10.\n")
                          else:
                              break #this break would break the while True above
                          print("Okay. Rolling the dice...")
                          numpick = randint(1, 10)
                          print("The number chosen is " + str(numpick) + ".")
                          if numpick == darenum:
                              print("Whoops! The number you chose was the one that we landed on! Everyone agree on a dare for " + playerturn + "!\n\n")
                              input("Press Enter once " + playerturn + " has done a dare...\n")
                          else:
                              print(playerturn + " has escaped! Moving on to the next person.\n\n") 
                              #there should be a break in here, otherwise the function would be stuck in the second while True
      

      固定的代码,改变了我在上面的cmets中提到的:

      # Pass Or Dare
      
      from random import randint
      firsttry = True
      def playerpicked(list):
          listwords = len(list)
          numpicked = randint(0, listwords - 1)
          userpicked = list[numpicked]
          return userpicked
      
      while firsttry:
          try:
              playercount = int(input('How many players will there be?\n'))
          except ValueError:
              print("That isn't a number. Please enter a number without decimals.")
          else:
              firsttry = False
              playernames = [input("Name of Player {}: ".format(i)) for i in range(1, playercount + 1)]
              while True:
                  playerturn = playerpicked(playernames)
                  print("The Player picked is:",playerturn)
                  while True:
                      try:
                          darenum = int(input(playerturn + ", which number do you NOT want to land on?\n"))
                      except ValueError:
                          print("Please enter a number.")
      
                      if darenum > 10 or darenum < 1:
                          print("Please enter a number between 1 and 10.\n")
      
                      else:
      
                          print("Okay. Rolling the dice...")
                          numpick = randint(1, 10)
                          print("The number chosen is " + str(numpick) + ".")
      
                          if numpick == darenum:
                              print("Whoops! The number you chose was the one that we landed on! Everyone agree on a dare for " + playerturn + "!\n\n")
                              input("Press Enter once " + playerturn + " has done a dare...\n")
      
                          else:
                              print(playerturn + " has escaped! Moving on to the next person.\n\n")
                              break
      

      【讨论】:

      • 刚刚用更多细节改进了答案。查看第一个代码的 cmets,然后检查第二个。主要是ifelsebreak被误用
      猜你喜欢
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 2018-10-19
      • 2014-02-12
      相关资源
      最近更新 更多