Kar*_*Lee 3 python dictionary lookup-tables python-3.x
到目前为止我所拥有的:
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)
我的问题是该程序允许用户输入他们的选择,但该函数将根据字典调用(不会运行)并且程序只会不断询问用户他们的选择。
小智 6
self.inputDict[choice]只会从 dict 中获取函数,而不会调用它,所以也许你需要替换self.inputDict[choice]为self.inputDict[choice]()