If we need to get the dictionary value, we have two methods, one is through dict[' key ', and the other is the Dict.get () method.
What we are sharing today is the dictionary's Get () method.
Here we can use a dictionary to make a small game, assuming the user in the terminal input string: "1" or "2" or "3", return the corresponding content, if it is input other, then return "error"
Some friends here may use if elif else judgment statement to operate, indeed can, but more cumbersome. It is very convenient to recommend the Get () method of a dictionary to everyone.
info = {' 1 ': ' First ', ' 2 ': ' Second ', ' 3 ': ' Third '}
Number = raw_input (' input type ' you number: ')
Print info.get (number, ' ERROR ')
This way, a single line of code get will be able to complete the Python judgment statement a few lines of code.
Python dictionary (dict) Get method application