【问题标题】:Issues with my number guessing game in python我在 python 中的猜数字游戏的问题
【发布时间】:2015-11-08 07:01:45
【问题描述】:

我是编程新手,所以请原谅下面的混乱......我正在尝试编写一个猜数字游戏。计算机应该随机生成一个介于 1 到 10 之间的数字(包括 1 到 10)。用户只允许 3 次尝试正确猜出数字。一个用户要么猜对了要么用完了尝试,我应该让程序询问用户他们是否想再次玩游戏,并且游戏应该重新开始。下面是我想出的。我认为我让这变得比它需要的复杂得多......我做错了什么,因为它不起作用?

import random


number = random.randint(1,10)


print "The computer will generate a random number between 1 and 10. Try to guess the number!"

guess = int(raw_input("Guess a number: "))
attempts = 0


while guess != number and attempts < 4:
    if guess >= 1 and guess <= 10:
       print "Sorry, you are wrong."
    else:
       print "That is not an integer between 1 and 10 (inclusive)."
       guess = int(raw_input("Guess another number: "))
       attempts = attempts + 1
        if attempts > 4:
           print "You've guessed incorrectly and are out of tries..."
           playAgain = raw_input("Would you like to play again? ")
          if playAgain = "Yes" or playAgain == "y":
            import random
            number = random.randint(1,10)
            attempts = 0
            guess = int(raw_input("Guess a number: "))
            while guess != number and attempts < 4:
                if guess >= 1 and guess <= 10:
                    print "Sorry, you are wrong."
                else:
                    print "That is not an interger between 1 and 10 (inclusive)."
                    guess = int(raw_input("Guess another number: "))
                    attempts = attempts + 1
while guess == number:                        
    print "Congratulations, you guessed correctly!"
    playAgain = raw_input("Would you like to play again? ")
        if playAgain = "Yes" or playAgain == "y":
            import random
            number = random.randint(1,10)
            attempts = 0
            guess = int(raw_input("Guess a number: "))
            while guess != number and attempts < 4:
                if guess >= 1 and guess <= 10:
                    print "Sorry, you are wrong."
                else:
                    print "That is not an interger between 1 and 10 (inclusive)."
                    guess = int(raw_input("Guess another number: "))
                    attempts = attempts + 1
                    if attempts > 3:
                       print "You've guessed incorrectly and are out of tries..."
                       playAgain = raw_input("Would you like to play again? ")
                          if playAgain == "yes" or playAgain == "Yes":
                             import random
                             number = random.randint(1,10)
                             attempts = 0
                             guess = int(raw_input("Guess a number: "))
                             while guess != number and attempts < 4:
                                  if guess >= 1 and guess <= 10:
                                     print "Sorry, you are wrong."
                                  else:
                                       print "That is not an interger between 1 and 10 (inclusive)."
                                       guess = int(raw_input("Guess another number: "))
                                       attempts = attempts + 1
                                       if attempts > 3:
                                          print "You've guessed incorrectly and are out of tries..."
                                          playAgain = raw_input("Would you like to play again? ")
                                          if playAgain == "yes" or playAgain == "Yes":
                                             import random
                                             number = random.randint(1,10)           

【问题讨论】:

标签: python python-2.7


【解决方案1】:

对于初学者来说有点不太先进的解决方案:

import random

attempts = 0

number = random.randint(1,10)    

while attempts < 4:
    attempts += 1
    print number       #print number to help with testing
    guess = int(raw_input("Guess a number from 1 to 10: "))
    if guess == number:
        print "you guessed the number!", 
        again = raw_input("do you want to play again? y or n  ")      
        if again == "y":
            number = random.randint(1,10)
            attempts = 0    # gives 4 attempts to a new game
        else:
            print "good bye"                
            break    
    elif attempts == 4:
        print "The game is over!"

【讨论】:

  • 我认为这不是一个有效的解决方案...您提示每次重播两次输入一个数字,并为每次尝试生成一个新的随机数。
  • @cricket_007 询问用户是否想再次玩开始一个新游戏所以你想生成一个新的随机数,新的游戏新的号码!
  • 当然,但是你 raw_input 在那里并且在同时的顶部。此外,该游戏还进行了 3 次猜测相同数字的尝试。
  • @cricket_007 感谢您的帮助,我将尝试更改为 0,我认为现在看起来很完美
【解决方案2】:

我认为你必须检查循环是如何工作的,你不能真正手动做事情,检查这个代码以获取循环和函数的实现示例:

import random

def guessGame():
    guessnum = random.randint(1,10)
    print "Try to guess the number between 1 and 10 in 3 ansers or less"
    i = 1
    while i < 4:
        try:
            num = int(raw_input("Attempt " + str(i) + ": "))
            if num == guessnum:
                return True
            else:
                i+=1
                print "Try again"
        except Exception as e:
            print "Please input a valid number"
            continue

def mainLoop():
    while True:
        guessGame()
        exitopt = raw_input("Good game, wanna try again? y - yes, n - no: ")
        if exitopt == "y":
            continue
        elif exitopt == "n":
            print "Bye!!!"
            break

if __name__ == "__main__":
    mainLoop()

【讨论】:

    猜你喜欢
    • 2021-05-29
    • 1970-01-01
    • 2016-06-21
    • 2013-10-09
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2021-05-18
    • 2019-02-07
    相关资源
    最近更新 更多