2010-05-08 179 views
3

我遇到了一個我編寫的簡單程序的問題,我想根據用戶輸入執行某個功能。我已經使用字典作爲替代交換機做任務,但是當我嘗試將功能分配到字典中不執行他們... 代碼:如何在字典中分配函數?

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 

我做了什麼錯誤?它看起來和我見過的所有例子一樣。......

回答

10

你忘了叫它。

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

PrintTables[ans.lower()]()