Guest User

Untitled

a guest
Jan 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. # create a mapping of state to abbreviation
  2. states = {
  3. 'Oregon': 'OR',
  4. 'Florida': 'FL',
  5. 'California': 'CA',
  6. 'New York': 'NY',
  7. 'Michigan': 'MI'
  8. }
  9.  
  10. # create a basic set of states and some cities in them
  11. cities = {
  12. 'CA': 'San Francisco',
  13. 'MI': 'Detroit',
  14. 'FL': 'Jacksonville'
  15. }
  16.  
  17. print '-' * 10
  18. # safely get a abbreviation by state that might not be there
  19. state = states.get('Texas', None)
  20.  
  21. if not state:
  22. print "Sorry, no Texas."
  23.  
  24. # get a city with a default value
  25. city = cities.get('TX', 'Does Not Exist')
  26. print "The city for the state 'TX' is: %s" % city
  27.  
  28. try:
  29. state = states['Texas']
  30. except KeyError:
  31. state = None
Add Comment
Please, Sign In to add comment