Python函数在调用时不起作用

时间:2016-04-10 17:13:24

标签: python

我正在学习Python,正在开发一个简单的猜数游戏。基本前提是使用提示输入一个数字然后被告知猜测太高,太低或正确。一旦猜到正确的号码,就会询问用户是否想要再次播放。但是,当我键入y时,程序会退出以提示而不是重新启动。我已经修改了用户提示和while,如果部分和一切都工作,除了用户输入y再次播放时应该发生的部分。

这是我的代码:

import random

def playGame():
    number = random.randrange( 1, 11 )
    return number

chance = playGame()

print "\nI am thinking of a number between 1-10."
print "Can you guess my number?"
guess = int( raw_input( "Enter your guess. " ) )

while guess != chance:

    if guess > chance:
        print "Too high. Try again."
        guess = int( raw_input ("\nEnter your guess. " ) )


    if guess < chance:
        print "Too low. Try again."
        guess = int( raw_input ("\nEnter your guess. " ) )


    if guess == chance:
        print "Congratulations! You guessed my number!"

answer = raw_input( "\nWould you like to try again? (y or n) " )    
if answer == 'n':
    print "\nThank you for playing!"

if answer == 'y':
    playGame()

如果我输入n,则正确打印出该字符串。但是当我点击y时程序就会退出。我在网上搜索过,但我发现的一切都说我只需要键入functionName(),它应该循环回到函数并重复,但它对我不起作用。没有最大数量的尝试来猜测这个数字。我已经设定它一直猜测,直到正确猜到。

我错过了什么?

谢谢!

2 个答案:

答案 0 :(得分:3)

您需要将整个游戏放入playGame()函数中:

import random

def playGame():
    chance = random.randrange(1, 11)

    print "\nI am thinking of a number between 1-10."
    print "Can you guess my number?"
    guess = int( raw_input( "Enter your guess. " ) )

    while guess != chance:
        if guess > chance:
            print "Too high. Try again."
            guess = int( raw_input ("\nEnter your guess. " ) )

        if guess < chance:
            print "Too low. Try again."
            guess = int( raw_input ("\nEnter your guess. " ) )

    print "Congratulations! You guessed my number!"

playGame()

keep_playing = True
while keep_playing:
    answer = raw_input( "\nWould you like to try again? (y or n) " )    
    if answer == 'n':
        print "\nThank you for playing!"
        keep_playing = False

    if answer == 'y':
        playGame()

否则你只需重置随机数 - 文件的其余部分不会再次自动运行,只有函数体。

答案 1 :(得分:-1)

我认为你可能会误解return语句在Python中的行为。 playGame()只返回一个随机数。在您点击该函数中的return语句后,您的代码将返回到调用它的位置,这是脚本的底部。

所以,流程看起来像这样。我们从这里开始:

if answer == 'y':
    playGame() # we enter the playGame() function

然后去这里:

def playGame():
    number = random.randrange( 1, 11 )
    return number # We get here, and return BACK to where we were called from

现在回到我们打电话的地方:

if answer == 'y':
    playGame()
    # The program continues...but nothing to execute here, so terminate
srowland的回答只允许你重复一次。这些“重复”问题的一个常见模式是将需要重复的所有代码包装到函数调用中。然后,当满足重复条件时,只需调用该函数即可。如果你想无限期地重复直到用户输入'n',那么你可能想要这样:

import random

def playGame():
    number = random.randrange( 1, 11 )
    return number

def start_my_game():
    chance = playGame()

    print "\nI am thinking of a number between 1-10."
    print "Can you guess my number?"
    guess = int( raw_input( "Enter your guess. " ) )

    while guess != chance:

        if guess > chance:
            print "Too high. Try again."
            guess = int( raw_input ("\nEnter your guess. " ) )


        if guess < chance:
            print "Too low. Try again."
            guess = int( raw_input ("\nEnter your guess. " ) )


        if guess == chance:
            print "Congratulations! You guessed my number!"

    answer = raw_input( "\nWould you like to try again? (y or n) " )
    if answer == 'n':
        print "\nThank you for playing!"

    if answer == 'y':
        start_my_game() # Start the game again

# Start the game
start_my_game()