简体   繁体   中英

Define or change command prompt with variable in Python

I'm using Python's cmd class in my app. I'd like to be able to define cmd.prompt() on the fly, but can't find a way to do it. This is what I've got, which doesn't seem to be working:

gamestate = 'adventure' #gamestate is either 'adventure' or 'battle'

def changestate(arg):
    global gamestate
    print('Right now the gamestate is {}'.format(gamestate))
    if not arg == '':
        print("Now we are changing the gamestate from {} to {}".format(gamestate, arg.lower()))
        gamestate = arg.lower()
        print('We have changed the gamestate to {}'.format(gamestate))
    else:
        print('{} isn\'t a valid gamestate'.format(arg.upper()))

class AdventureCmd(cmd.Cmd):    
    global gamestate
    if gamestate == 'adventure':
        prompt = '\nWhat do you do?\n'
    else:
        prompt = '\nWhat do you battle?\n' #this lets us know we are now in 'battle' gamestate  

    def do_changestate(self,arg):
        changestate(arg) #'changestate battle' changes the gamestate to 'battle'

if __name__ == '__main__':
    AdventureCmd().cmdloop()

This is the output I get:

What do you do?
changestate adventure
Right now the gamestate is adventure
Now we are changing the gamestate from adventure to adventure
We have changed the gamestate to adventure

What do you do?
changestate battle
Right now the gamestate is adventure
Now we are changing the gamestate from adventure to battle
We have changed the gamestate to battle

What do you do? #should be 'What do you battle'

I'm just a python n00b so it may have something to do with modifying superclasses or something like that I don't know how to do yet. Can you guys give me some advice?

EDIT: I've also tried:

class AdventureCmd(cmd.Cmd):
    global gamestate
    def preloop(self):
        if gamestate == 'adventure':
            self.prompt = '\nWhat do you do?'
        elif gamestate == 'battle':
            self.prompt = '\nWhat do you battle?'

Most of your code is only run once, when the AdventureCmd class is defined. It is not run each time a user provides input. Specifically, this code is only run once when defining the class:

global gamestate
if gamestate == 'adventure':
    prompt = '\nWhat do you do?\n'
else:
    prompt = '\nWhat do you battle?\n' #this lets us know we are now in 'battle' gamestate

As a result, you never redefine the prompt variable. You can do that like this:

class AdventureCmd(cmd.Cmd):
global gamestate if gamestate == 'adventure': prompt = '\\nWhat do you do?\\n' else: prompt = '\\nWhat do you battle?\\n' #this lets us know we are now in 'battle' gamestate

def do_changestate(self,arg):
    changestate(arg)
    # Note we're setting self.prompt here as the prompt is an
    # instance variable for this instance of AdventureCmd. Note also
    # that we change the prompt in the method that gets called to
    # handle the command
    if gamestate == 'adventure':
        prompt = '\nWhat do you do?\n'
    else:
        prompt = '\nWhat do you battle?\n' #this lets us know we are now in 'battle' gamestate  

There's a few other changes you might consider in your code. For example, it's probably a good idea to make the gamestate an instance variable rather than a global and it will help with extensibility if instead of an if/elif loop chain for changing the prompt you have a dictionary mapping the gamestate to correct prompt.

Note: I didn't test the above code so there may be typos or other errors but I think the basic idea is right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM