小编Kar*_*Lee的帖子

Python:如何使用字典调用方法(字典中的值)以根据用户输入(字典中的键)在不同的函数中运行?

到目前为止我所拥有的:

def run_A():
   # code...

def run_B():
   # code...

def run_C():
   # code...

inputDict = {'a': run_A, 'b': run_B, 'c': run_C, 'q': False}  # q for quit program
def userChoice(self):
    y = True
    while y:
       choice = input("a. run A\nb. run B\nc. run C\nq. Quit").lower()
       self.inputDict[choice]
Run Code Online (Sandbox Code Playgroud)

我的问题是该程序允许用户输入他们的选择,但该函数将根据字典调用(不会运行)并且程序只会不断询问用户他们的选择。

python dictionary lookup-tables python-3.x

3
推荐指数
1
解决办法
823
查看次数

标签 统计

dictionary ×1

lookup-tables ×1

python ×1

python-3.x ×1