This example describes the Python dictionary get () method usage. Share to everyone for your reference. The specific analysis is as follows:
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.
The dictionary's Get () method is what we share here.
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"
>>> 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 ' Nu Mber: ') input type you number:4>>> print info.get (number, ' ERROR ') error
Hopefully this article will help you with Python programming.