如果我們需要獲取字典值的話,我們有兩種方法,一個是通過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