Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # create a mapping of state to abbreviation
- states = {
- 'Oregon': 'OR',
- 'Florida': 'FL',
- 'California': 'CA',
- 'New York': 'NY',
- 'Michigan': 'MI'
- }
- # create a basic set of states and some cities in them
- cities = {
- 'CA': 'San Francisco',
- 'MI': 'Detroit',
- 'FL': 'Jacksonville'
- }
- print '-' * 10
- # safely get a abbreviation by state that might not be there
- state = states.get('Texas', None)
- if not state:
- print "Sorry, no Texas."
- # get a city with a default value
- city = cities.get('TX', 'Does Not Exist')
- print "The city for the state 'TX' is: %s" % city
- try:
- state = states['Texas']
- except KeyError:
- state = None
Add Comment
Please, Sign In to add comment