我的猜数游戏在python

时间:2015-11-07 21:18:20

标签: python python-2.7

我是编程的新手所以请原谅下面的混乱......我正在尝试编写一个数字猜谜游戏。计算机应该随机生成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)           

2 个答案:

答案 0 :(得分: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!"

答案 1 :(得分:0)

我认为你必须检查循环是如何工作的,你不能手动做事,检查这段代码是否有一个带循环和函数的实现示例:

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()