Python函数在调用时不工作

2024-05-19 03:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python,正在做一个简单的数字猜测游戏。基本前提是使用提示输入一个数字,然后被告知猜测要么过高,要么过低,要么正确。一旦猜到了正确的数字,就会询问用户是否想再玩一次。但是,当我键入y时,程序退出并返回提示,而不是重新启动。我已经修改了用户提示和while和if部分,以及除了用户键入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(),它应该循环回到函数并重复,但对我不起作用。猜测数字的次数没有上限。我把它设置为不断猜测,直到猜对为止。你知道吗

我错过了什么?你知道吗

谢谢!你知道吗


Tags: 用户younumberinputyourraw键入if
2条回答

我认为您可能误解了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

斯洛兰的答案只允许你重复一次。这些“重复”问题的一个常见模式是将所有需要重复的代码包装到函数调用中。然后,当满足重复的条件时,只需调用该函数。如果要无限期重复,直到用户输入“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()

您需要将整个游戏放入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()

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

相关问题 更多 >

    热门问题