有没有办法选择字典,然后从该字典中选择一个函数?

我试图通过用户输入选择一个字典,然后从中选择一个在所选字典中列出的函数来运行。我的代码发生了什么是它将正确地选择正确的字典,但是在挑选函数时,它将在输入所需的函数ID时工作,但在所选词典中重复每个项目条目。我需要做什么来解决这个问题?


实际定义的函数只是在测试/调试时打印一个随机数作为占位符。此外,为了在测试/调试时简单起见,我将函数保留在其十六进制内存位置。


calculus = {

  '1': cal1,

  '2': cal2,

  '3': cal3,

  }

physics = {

  '1': phy1,

  '2': phy2,

  '3': phy3,

  }


def Main():

  a = input("Choose equation set. Calculus or Physics: ")

  if a == "Calculus":

    for item in (calculus.keys()):

      print(item,":",calculus.get(item,'-'))

      eq_id = input("Enter desired equation function id: ")

      eq_arg = input("Press Enter")

      calculus[eq_id](eq_arg)

  elif a == "Physics":

    for item in (physics.keys()):

      print(item,":",physics.get(item,'-'))

      ph_id = input("Enter desired equation id: ")

      ph_arg = input("Press Enter")

      physics[ph_id](ph_arg)

这是输出给我的示例:(“”中的用户输入,“ ”中的函数名称,“ ”中的输出)


选择方程组。微积分或物理学:“物理学”

1:(函数 phy1 在 0x7f32a527c6a8)

输入所需的方程 id:“1”

按 Enter

'“347”'

2 :(函数 phy2 在 0x7f32a527c730)

输入所需的方程 id:“3”

按 Enter

'” 540"'

3 : (function phy3 at 0x7f32a527c7b8)

输入所需的方程 id: "2"

Press Enter

'"429"'


但我希望它更像这样:


选择方程组。微积分或物理学:“物理学”

1:“物理学 1”

2:“物理学 2”

3:“物理学 3”

输入所需的方程 ID:“2”

'“执行选择的函数”'


慕森卡
浏览 174回答 3
3回答

慕工程0101907

添加带有函数名称的嵌套字典:calculus = {&nbsp; '1': {'Name': 'Calculus 1', 'Function': cal1},&nbsp; '2': {'Name': 'Calculus 2', 'Function': cal2},&nbsp; '3': {'Name': 'Calculus 3', 'Function': cal3}&nbsp; }然后修改你的循环:for key, item in calculus.items():&nbsp; print(key,":", item.get('Name'))eq_id = input("Enter desired equation function id: ")# eq_arg = input("Press Enter") # <--- (You don't need this)calculus.get(key).get('Function')()您也可以使用嵌套的lists 代替,['Calculus 1', cal1]但可能更难分辨它calculus['1'][0]是名称calculus['1'][1]还是函数。如果您只关心一个根据出现顺序递增的通用函数名称,那么它就更简单了:for item in (calculus.keys()):&nbsp; print(item,":", 'Calculus {}'.format(item)))&nbsp; eq_id = input("Enter desired equation function id: ")&nbsp; calculus.get(eq_id)()

开满天机

看起来您在这里想要的是一个嵌套字典,其中顶级键是“Physics”和“Calculus”,然后每个的值将是您已经指定的字典。formulas = { "Calculus" :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'1': cal1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'2': cal2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'3': cal3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Physics" :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'1': phy1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'2': phy2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'3': phy3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}这样您就可以在同一个函数中完成所有输入。a = input("Choose equation set. Calculus or Physics: ")formula_set = forumlas[a]for key in forumla_set.keys():&nbsp; print("{}: {} {}".format(key, a, key)eq_id = input("Enter desired equation function id: ")eq_arg = input("Press Enter")formula_set[eq_id](eq_arg)正如提到的评论之一,最好将字典键设为小写,然后将所有输入转换为小写。

守候你守候我

最简单的事情是,以下内容:calculus = {&nbsp; '1': calculus_1,&nbsp; '2': calculus_2,&nbsp; '3': calculus_3,&nbsp; }physics = {&nbsp; '1': physics_1,&nbsp; '2': physics_2,&nbsp; '3': physcis_3,&nbsp; }def Main():&nbsp; a = input("Choose equation set. Calculus or Physics: ")&nbsp; if a == "Calculus":&nbsp; &nbsp; for item in (calculus.keys()):&nbsp; &nbsp; &nbsp; # I'm using the __name__ attribute on the function handle&nbsp; &nbsp; &nbsp; print(item,":",calculus.get(item,'-').__name__)&nbsp; &nbsp; &nbsp; eq_id = input("Enter desired equation function id: ")&nbsp; &nbsp; &nbsp; eq_arg = input("Press Enter")&nbsp; &nbsp; &nbsp; calculus[eq_id](eq_arg)&nbsp; elif a == "Physics":&nbsp; &nbsp; for item in (physics.keys()):&nbsp; &nbsp; &nbsp; print(item,":",physics.get(item,'-').__name__) # i'm using the __name__ attribute on the function handle&nbsp; &nbsp; &nbsp; ph_id = input("Enter desired equation id: ")&nbsp; &nbsp; &nbsp; ph_arg = input("Press Enter")&nbsp; &nbsp; &nbsp; physics[ph_id](ph_arg)但我建议,您将数据结构更改为如下所示;calculus = {&nbsp; &nbsp; &nbsp; '1': {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'func_name': 'calculus 1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'func': calculus_1 # where calculus_1 is the function handle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; '2': {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'func_name': 'calculus 2',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'func': calculus_2 # where calculus_2 is the function handle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; '3': {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'func_name': 'calculus 3',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'func': calculus_3 # where calculus_3 is the function handle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }并通过以下方式访问它,例如,for item in (calculus.keys()):&nbsp; &nbsp; print(item,":",calculus.get(item,'-')['func_name'])&nbsp; &nbsp; ...&nbsp; &nbsp; calculus[eq_id]['func'](eq_arg)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python