本文實例講述了python字典get()方法用法。分享給大家供大家參考。具體分析如下:
如果我們需要獲取字典值的話,我們有兩種方法,一個是通過dict['key'],另外一個就是dict.get()方法。
這里給大家分享的就是字典的get()方法。
這里我們可以用字典做一個小游戲,假設用戶在終端輸入字符串:"1"或者是"2"或者是"3",返回對應的內容,如果是輸入其他的,則返回"error"
>>> info = {'1':'first','2':'second','3':'third'}>>> number = raw_input('input type you number:')input type you number:3>>> print info.get(number,'error')third>>> number = raw_input('input type you number:')input type you number:4>>> print info.get(number,'error')error
希望本文所述對大家的Python程序設計有所幫助。
新聞熱點
疑難解答