Unknown reason for code executing the way it works in python

I am a novice programmer using python on Mac.

I created a function as part of a game that receives player input for the name of the main character.

The code:

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 tested, I entered "major a", and when he asked me to enter a different name, I entered "a b". However, when it returns the output of the function, it returns "major a". I went through this with a debugger, but I still cannot find where the problem occurred.

Thanks for any help, Jasper

+3
source share
1 answer

newGameStep2() , , , if/else, return newStep2Choice . :

return newGameStep2()
+8

Source: https://habr.com/ru/post/1740639/


All Articles