使用用户输入来调用功能

使用用户输入来调用功能,第1张

使用用户输入来调用功能

看起来您正在使用python3.x,其中

input
返回了一个字符串。要恢复python2.x行为,您需要
eval(input())
。但是,您不应该这样做。这可能会导致糟糕的一天。


一个更好的主意是将函数放入字典中-

def move():    #...def jump():    #...function_dict = {'move':move, 'jump':jump }

接着:

func = input('>')  #raw_input on python2.xfunction_dict[func]()

以下代码在python3.2上对我有用。

def move():    print("Test.")func_dict = {'move':move}if __name__ == "__main__":    input("Press enter to begin.")    currentEnvironment = "room" #getNewEnvironment(environments)    currentTimeOfDay = "1 A.M." #getTime(timeTicks, timeOfDay)    print("You are standing in the {0}. It is {1}.".format(currentEnvironment, currentTimeOfDay))    command = input("> ")    func_dict[command]()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5642784.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存