Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python中的变量定义或更改命令提示符_Python - Fatal编程技术网

用Python中的变量定义或更改命令提示符

用Python中的变量定义或更改命令提示符,python,Python,我在我的应用程序中使用Python的cmd类。我希望能够动态地定义cmd.prompt(),但找不到方法。这就是我得到的,似乎不起作用: gamestate = 'adventure' #gamestate is either 'adventure' or 'battle' def changestate(arg): global gamestate print('Right now the gamestate is {}'.format(gamestate)) if

我在我的应用程序中使用Python的cmd类。我希望能够动态地定义cmd.prompt(),但找不到方法。这就是我得到的,似乎不起作用:

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()
这是我得到的输出:

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'
我只是一个PythonN00B,所以它可能与修改超类或类似的东西有关,我还不知道如何做。你们能给我一些建议吗

编辑:我也尝试过:

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?'

当定义了
AdventureCmd
类时,大多数代码只运行一次。它不是每次用户提供输入时都运行的。具体来说,在定义类时,此代码只运行一次:

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
因此,您永远不会重新定义
prompt
变量。您可以这样做:

类AdventureCmd(cmd.cmd):
全局配子状态 如果游戏状态==“冒险”: prompt='\n您做什么?\n' 其他: prompt='\n你在战斗什么?\n'#这让我们知道我们现在处于“战斗”游戏状态

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  
<>你可以在代码中考虑其他一些变化。例如,将游戏状态设置为实例变量而不是全局变量可能是一个好主意,如果您有一个字典将游戏状态映射到正确的提示,那么它将有助于扩展性,而不是用于更改提示的if/elif循环链


注意:我没有测试上面的代码,因此可能会有打字错误或其他错误,但我认为基本想法是正确的。

当定义了
AdventureCmd
类时,大多数代码只运行一次。它不是每次用户提供输入时都运行的。具体来说,在定义类时,此代码只运行一次:

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
因此,您永远不会重新定义
prompt
变量。您可以这样做:

类AdventureCmd(cmd.cmd):
全局配子状态 如果游戏状态==“冒险”: prompt='\n您做什么?\n' 其他: prompt='\n你在战斗什么?\n'#这让我们知道我们现在处于“战斗”游戏状态

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  
<>你可以在代码中考虑其他一些变化。例如,将游戏状态设置为实例变量而不是全局变量可能是一个好主意,如果您有一个字典将游戏状态映射到正确的提示,那么它将有助于扩展性,而不是用于更改提示的if/elif循环链

注意:我没有测试上面的代码,所以可能会有打字错误或其他错误,但我认为基本想法是正确的