【问题标题】:Python Random number guesser problem with loops带有循环的Python随机数猜测器问题
【发布时间】:2021-02-01 15:24:41
【问题描述】:

我已经构建了一个随机数猜谜游戏来练习,但我在最后的步骤中遇到了一些问题。输出时,游戏按预期运行,但是,我希望它询问用户是否要在每次猜测轮次后再次玩,“是”表示游戏继续进行,“退出”表示游戏停止。到目前为止,游戏要求用户猜数字,如果数字不匹配,则告诉用户,然后询问他们是否要玩,然后只是重复猜测部分,而不询问用户是否要再次玩。这不是我想要的,因为我想知道如何正确编写这段代码。 这是我的程序:

    import random

    guess = int(input("Guess the number => "))
    rand = random.randrange(1,10)
    print("The number was", rand)
    def guess_rand(guess, rand):
        if rand == guess:
            print("You have guessed right!")
        elif rand > guess:
            print("You guessed too low!")
        elif guess > rand:
            print("You guessed too high!")
    
    guess_rand(guess, rand)

    again = input("Would you like to try again? => ")
    while again.lower() == 'yes':
        guess = int(input("Guess the number => "))
        rand = random.randrange(1,10)
        print("The number was", rand)
        guess_rand(guess, rand)
        if again.lower() == 'exit':
          break

另外,如果有关于如何跟踪用户进行了多少次猜测以及在游戏结束时将其打印出来的任何提示,我将不胜感激。谢谢。

【问题讨论】:

    标签: python python-3.x loops random input


    【解决方案1】:

    您在while 循环中缺少再次获取用户输入的语句:

    again = input("Would you like to try again? => ")
    while again.lower() == 'yes':
        guess = int(input("Guess the number => "))
        rand = random.randrange(1,10)
        print("The number was", rand)
        guess_rand(guess, rand)
        again = input("Would you like to try again? => ")
        if again.lower() == 'exit':
        break
    

    为了保持计数,您可以在while 循环中添加一个新变量并递增。

    【讨论】:

    • 谢谢,我已经解决了我遇到的两个问题。
    • 真棒@FranPY3,请考虑接受将问题标记为已解决的答案。
    【解决方案2】:

    这是你问的,我还添加了轮流来跟踪玩家

    import random
    turns = 0
    def quit():
        i = input ("Do you want to play again? if yes enter Yes if not Enter No\n")
        if (i.lower() not in ["yes","no"]):
            print ("Invalid input")
            return True
        if (i.lower() == "yes"):
            print ("You choose to play")
            return False
        else:
            print ("Thankyou for playing")
            return True
    while True:
        guess = int(input("Guess the number => "))
        rand = random.randrange(1,10)
        print("The number was", rand)
        def guess_rand(guess, rand):
            if rand == guess:
                print("You have guessed right!")
            elif rand > guess:
                print("You guessed too low!")
            elif guess > rand:
                print("You guessed too high!")
        
        guess_rand(guess, rand)
        
        if quit():
            print("You have used",turns,"trun(s)")
            break
        else:
            turns += 1
            continue
    

    【讨论】:

      【解决方案3】:

      我刚刚在 while 循环中添加了“再次”代码行并添加了一条 else 语句

      import random
      
      guess = int(input("Guess the number => "))
      rand = random.randrange(1,10)
      print("The number was", rand)
      def guess_rand(guess, rand):
          if rand == guess:
              print("You have guessed right!")
          elif rand > guess:
              print("You guessed too low!")
          elif guess > rand:
              print("You guessed too high!")
      
      guess_rand(guess, rand)
      
      again = input("Would you like to try again? => ")
      while again.lower() == 'yes':
          guess = int(input("Guess the number => "))
          rand = random.randrange(1,10)
          print("The number was", rand)
          guess_rand(guess, rand)
          
          again = input("Would you like to try again? => ")
          if again.lower() == 'exit':
            break
          else:
              continue
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多