代码以python中的方式执行的原因未知

2024-09-28 22:29:10 发布

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

我是一个初学者程序员,在Mac上使用python。你知道吗

我创建了一个函数作为游戏的一部分,它接收玩家输入的主角的名字。你知道吗

代码是:

import time

def newGameStep2():
        print '  ******************************************  '
        print '\nStep2\t\t\t\tCharacter Name'
        print '\nChoose a name for your character. This cannot\n be changed during the game. Note that there\n are limitations upon the name.'
        print '\nLimitations:\n\tYou cannot use:\n\tCommander\n\tLieutenant\n\tMajor\n\t\tas these are reserved.\n All unusual capitalisations will be removed.\n There is a two-word-limit on names.'
        newStep2Choice = raw_input('>>>')
        newStep2Choice = newStep2Choice.lower()
        if 'commander' in newStep2Choice or 'lieutenant' in newStep2Choice or 'major' in newStep2Choice:
            print 'You cannot use the terms \'commander\', \'lieutenant\' or \'major\' in the name. They are reserved.\n'
            print
            time.sleep(2)
            newGameStep2()
        else:
            newStep2Choice = newStep2Choice.split(' ')
            newStep2Choice = [newStep2Choice[0].capitalize(), newStep2Choice[1].capitalize()]
            newStep2Choice = ' ' .join(newStep2Choice)
        return newStep2Choice

myVar = newGameStep2()
print myVar

当我在测试的时候,我输入了'major a',当它要求我输入另一个名字时,我输入了'ab'。 但是,当它返回函数的输出时,它返回“major a”。我用调试器处理了这个问题,但是我仍然找不到问题发生的地方。你知道吗

谢谢你的帮助, 碧玉


Tags: orthe函数nameintimeusebe
1条回答
网友
1楼 · 发布于 2024-09-28 22:29:10

newGameStep2()的递归调用没有返回,因此当第二个调用完成时,控制流在if/else块之后的第一个调用中继续,并且return newStep2Choice返回第一个读取值。您需要将递归调用更改为:

return newGameStep2()

相关问题 更多 >