12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- # A simple database
- # A dictionary with person names as keys. Each person is represented as
- # another dictionary with the keys 'phone' and 'addr' referring to their phone
- # number and address, respectively.
- people = {
- 'Alice': {
- 'phone': '2341',
- 'addr': 'Foo drive 23'
- },
- 'Beth': {
- 'phone': '9102',
- 'addr': 'Bar street 42'
- },
- 'Cecil': {
- 'phone': '3158',
- 'addr': 'Baz avenue 90'
- }
- }
- # Descriptive labels for the phone number and address. These will be used
- # when printing the output.
- labels = {
- 'phone': 'phone number',
- 'addr': 'address'
- }
- name = input('Name: ')
- # Are we looking for a phone number or an address?
- request = input('Phone number (p) or address (a)? ')
- # Use the correct key:
- if request == 'p': key = 'phone'
- if request == 'a': key = 'addr'
- # Only try to print information if the name is a valid key in
- # our dictionary:
- if name in people: print("{}'s {} is {}.".format(name, labels[key], people[name][key]))
|