猜数游戏在第一次猜测后无法识别正确答案

时间:2015-02-09 18:04:45

标签: python if-statement while-loop

如果您在第一次尝试时没有正确猜测数字,程序将一直运行,直到您用完猜测而不管您是否猜到了正确的数字。我的while循环或if语句有问题吗?

while True:
    try:
        Guessed_number = int(raw_input("What is your guess? \n"))
    except ValueError: 
        print("Sorry, that is not a valid guess. Please guess again.")
        continue
    else:
        break 
def Number_guesser(Guessed_number):
    guesses= 3
    while guesses > 0: 

    if Random_number == Guessed_number:
        print "Congratualations! You won :)" 
        break

    elif Guessed_number > Random_number:
        print "You guessed higher than the number. Try again! \n"
        print Random_number
        Guessed_number= raw_input()
        guesses= guesses-1 

    elif Guessed_number < Random_number:
        print "You guessed lower than the number. Try again! \n"
        Guessed_number= raw_input() 
        guesses= guesses-1

    if guesses == 3:
        print "You have 3 guesses left!" 

    elif guesses == 2:
        print "You have 2 guesses left!"

    elif guesses == 1:
        print "You have 1 guess left!"

    elif guesses == 0:
        print "You ran out of guesses :( \n The correct answer was........*drum roll*" 
        print Random_number
print Number_guesser(Guessed_number)`

1 个答案:

答案 0 :(得分:1)

我发现每当我提示玩家再次猜测时,我的raw_input必须是一个整数。

Guessed_number= int(raw_input())