由於某種原因,while循環從不中斷,就好像userGuess永遠不會等於compAnswer一樣。我有它打印在一開始的答案,所以我們知道。在Pythonista上完成。Python猜測遊戲
def guessing_game():
compAnswer = random.randint(1,10)
print compAnswer
guesses = 1
print "Okay, I\'m thinking of a number between 1 and 10."
userGuess = raw_input("What number am I thinking of?: ")
while userGuess != compAnswer:
userGuess = raw_input("Nope! try again: ")
guesses += 1
playAgain = raw_input("You got it! My number was %s and it took you %d guesses. Play again?: " % (compAnswer, guesses))
if playAgain == "yes":
guessing_game()
else:
print "Okay bye!"
這裏使用了遞歸是沒有意義的,而不是進入一個新的功能,只是其中'playAgain =「是使用'while'循環「' – cmd
噢,這件事有我的一些功能同時也需要其他東西,但我知道如何完成這一點。 – teebles