由于某种原因,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