listing4-1.py 1012 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # A simple database
  2. # A dictionary with person names as keys. Each person is represented as
  3. # another dictionary with the keys 'phone' and 'addr' referring to their phone
  4. # number and address, respectively.
  5. people = {
  6. 'Alice': {
  7. 'phone': '2341',
  8. 'addr': 'Foo drive 23'
  9. },
  10. 'Beth': {
  11. 'phone': '9102',
  12. 'addr': 'Bar street 42'
  13. },
  14. 'Cecil': {
  15. 'phone': '3158',
  16. 'addr': 'Baz avenue 90'
  17. }
  18. }
  19. # Descriptive labels for the phone number and address. These will be used
  20. # when printing the output.
  21. labels = {
  22. 'phone': 'phone number',
  23. 'addr': 'address'
  24. }
  25. name = input('Name: ')
  26. # Are we looking for a phone number or an address?
  27. request = input('Phone number (p) or address (a)? ')
  28. # Use the correct key:
  29. if request == 'p': key = 'phone'
  30. if request == 'a': key = 'addr'
  31. # Only try to print information if the name is a valid key in
  32. # our dictionary:
  33. if name in people: print("{}'s {} is {}.".format(name, labels[key], people[name][key]))