我是初学程序员,在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
Run Code Online (Sandbox Code Playgroud)
当我测试时,我输入了'major a',当它要求我输入另一个名字时,我输入'a b'.但是,当它返回函数的输出时,它返回'major a'.我用调试器完成了这个,但我似乎无法找到问题发生的地方.
感谢任何帮助,贾斯珀
您的递归调用newGameStep2()未返回,因此当第二个调用完成时,控制流将在if/else块之后的第一个调用中继续,并return newStep2Choice返回第一个读取值.您需要将递归调用更改为:
return newGameStep2()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |