Using user input to call functions

前端 未结 4 1101
眼角桃花
眼角桃花 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:48

    It's usually better to re-use code as Hans suggests, but if you wanted to input commands and run them manually, it would be MUCH safer to have a dictionary of valid commands than to directly execute user-provided input.

    cmd = { 'move': move, 'jump': jump, 'look': look }
    

提交回复
热议问题