Using user input to call functions

前端 未结 4 1095
眼角桃花
眼角桃花 2020-12-06 15:11

I was trying to make a \"game\" in Python where the user inputs a command. However, I do not know whether you can take that input to be a function name. This is my current e

4条回答
  •  感情败类
    2020-12-06 15:55

    Have a look at the cmd module. See this.

    It is normally used for shell style comman dlanguages, but it can also be used to create simple text style adventure games.

    You can create commands by creating a new method on the Cmd subclass.

    E.g.

    def do_move(self, args):
        if self.next_room.has_snake():
            print "The next room contains a poisonous snake. It bites you and you die."
        else:
            print "The room is empty"
    

提交回复
热议问题