How to assign functions in the dictionary?

I had a problem with a simple program that I wrote, I want to execute a specific function according to user input. I already used the dictionary as a replacement for the switch for assignment, but when I try to assign functions to the dictionary, it does not execute them ... 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 have I done wrong? It looks the same as all the examples I've seen ...

+3
source share
1 answer

You forgot to call him.

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

or

PrintTables[ans.lower()]()
+10
source

Source: https://habr.com/ru/post/1744621/


All Articles