如何改进我的输入命令

2024-04-26 00:52:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找任何建议,将改善,简化,甚至整理我的输入命令。我知道重复不是一个好习惯。你知道吗

while(char.health > 0):
    print("_____________________________________")
    line = input("\n>").lower().split()
    if len(line) < 1:
        print ("{} doesn't understand the command?".format(char.name))
    elif line[0] == "go":
        commands.movement()
    elif line[0] == "quit":
        commands.quit()
    elif line[0] == "rest":
        commands.rest()
    else:
        print ("{} doesn't understand the command?".format(char.name))

我见过一些人用这样的口述:

    cmd = {'go' : commands.movement()} 

当我这样做时,它似乎运行def而不调用它们!你知道吗


已解决。
我已经想出了解决办法。你知道吗

cmd = { "go" : (commands.movement)}

这是我的新命令dict。我意识到commands.movement()正在调用def。你知道吗

while (char.health > 0):
    print("_________________________________________")
    line = input(">").lower().split()
    if len(line) < 1:
        print ("Invalid command!")
    elif line[0] in cmd:
        cmd[line[0]]()     #this now calls the def instead
    else:
        print("Invalid command!")

Tags: the命令cmdgoinputdeflinecommand

热门问题