【发布时间】:2014-05-12 16:38:01
【问题描述】:
由于某种原因,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!"
【问题讨论】:
-
这里使用递归是没有意义的,而不是进入一个新函数,只需使用
while循环 whereplayAgain != "yes" -
哦,是的,这东西有一些我正在开发的功能,同时还需要其他东西,但我知道如何完成。
标签: python pythonista