由于某种原因,while 循环永远不会中断,就好像 userGuess 永远不会等于 compAnswer。我让它在一开始就打印答案,所以我们知道。在 Pythonista 上完成。
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!"