CHAPTER 4 DICTIONARIES: WHEN INDICES (Free web hosting music) WON T DO
Friday, November 30th, 2007CHAPTER 4 DICTIONARIES: WHEN INDICES WON T DO 71 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 = raw_input(’Name: ‘) # Are we looking for a phone number or an address? request = raw_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’s %s is %s.” % (name, labels[key], people[name][key]) String Formatting with Dictionaries In Chapter 3, you saw how you could use string formatting to format all the values in a tuple. If you use a dictionary (with only strings as keys) instead of a tuple, you can make the string formatting even snazzier. After the % character in each conversion specifier, you add a key (enclosed in parentheses), which is followed by the other specifier elements:
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.