I am a beginner programmer, using python on Mac.
I created a function as a part of a game which receives the player’s input for the main character’s name.
The code is:
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
When I was testing, I inputted ‘major a’, and when it asked me to input another name, i inputted ‘a b’.
However, when it returned the output of the function, it returns ‘major a’. I went through this with a debugger, yet I still can’t seem to find where the problem occurred.
Thanks for any help,
Jasper
Your recursive call to
newGameStep2()isn’t returning, so when the second call finishes, control flow continues in the first call after the if/else block, andreturn newStep2Choicereturns the first read value. You need to change the recursive call to: