开发者

How do I assign functions in a dictionary?

开发者 https://www.devze.com 2022-12-29 09:36 出处:网络
I\'m having a problem with a simple program I wrote, I want to perform a certain function according to the users input. I\'ve already used a dictionary as a replacement for a switch to do assignment b

I'm having a problem with a simple program I wrote, I want to perform a certain function according to the users input. I've already used a dictionary as a replacement for a switch to do assignment but when I try to assign functions to the dictionary it doesn't execute them... The code:

def PrintValuesArea():
    ## do this
def PrintValuesLength():
    ## do that
def PrintValuesTime():
    ## do third

PrintTables={"a":PrintValuesArea,"l":PrintValuesLength,"t":PrintValuesTime}
PrintTables.get(ans.lower()) ## ans is the user input

what did I do wrong? It looks th开发者_StackOverflowe same as all the examples I've seen....


You forgot to call it.

PrintTables.get(ans.lower())()

or

PrintTables[ans.lower()]()
0

精彩评论

暂无评论...
验证码 换一张
取 消