Unknown reason for code executing the way it does in python
Posted
by Jasper
on Stack Overflow
See other posts from Stack Overflow
or by Jasper
Published on 2010-04-11T08:37:58Z
Indexed on
2010/04/11
8:43 UTC
Read the original article
Hit count: 183
Hi
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
© Stack Overflow or respective owner