This weeks lab is based on the example on pages 53,54 of the wikibook "Non-Programmers Tutorial For Python" by Josh Cogliati (2005), (see http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/Dictionaries).
In his example, Cogliati has options for printing, adding, removing, and looking up a phone number. Change the code so that, instead of the value in the dictionary being a simple phone number, it is now a list with three values:
- phone number
- e-mail
- address web page
The key should still be simply the persons name. Adapt the menu used in the example accordingly, for example the '2. Add a Phone Number' should now read '2. Add an entry' and if selected should ask the user for the 4 items of information (name, phone, email, web). Aditionally:
Add an option (e.g. number 6 in the menu) to 'Change/Edit an existing entry'.
Add options to:
- Print just a list of the phone numbers
- Print just a list of the e-mail addresses
- Print just a list of the web addresses
- Print all of the above together
This is the assignment we were given, I understand what's given in the link and have added a bit to it, unsure as to how to go about adding in the calling upon of the email and webpage information once stored
Although I agree with the comment under your answer, I will still try my best to give you some guidance.
Original Code:
def print_menu():print('1. Print Phone Numbers')print('2. Add a Phone Number')print('3. Remove a Phone Number')print('4. Lookup a Phone Number')print('5. Quit')print()numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 5:menu_choice = int(input("Type in a number (1-5): "))if menu_choice == 1:print("Telephone Numbers:")for x in numbers.keys():print("Name: ", x, "\tNumber:", numbers[x])print()elif menu_choice == 2:print("Add Name and Number")name = input("Name: ")phone = input("Number: ")numbers[name] = phoneelif menu_choice == 3:print("Remove Name and Number")name = input("Name: ")if name in numbers:del numbers[name]else:print(name, "was not found")elif menu_choice == 4:print("Lookup Number")name = input("Name: ")if name in numbers:print("The number is", numbers[name])else:print(name, "was not found")elif menu_choice != 5:print_menu()
Notice that numbers
is equal to {}
- this signifies that it is a "Dictionary", which stores key/value pairs. To add to a dictionary (or "dict"), you can modify it manually as such: numbers = {'David': 18003574689}
. So, in order to access David's phone number, you would type in numbers['David']
.
Another way to add to it is by instantiating it (which is already done for you via numbers = {}
), and then adding information into to it via the shortcut formula dictname['key'] = value
. So in this case, the shorthand can be numbers['Laura'] = 9173162546
.
Now, to add a list
into the mix, you could use []
(which is a list in python), but you would probably be better suited nesting another dict into the current one. For example, instead of numbers = {'David': 18003574689}
, you can now have numbers = {'David': {'phone number': 18003574689, 'e-mail': '[email protected]', 'address web page': 'http://dave.com'}, 'Laura': [...etc...]}
.
To access these new nested dicts, what you can do is the shorthand numbers['David']['phone number']
, which will return his #. You can then do this exact shortcode 2 more times numbers['David']['e-mail']
& numbers['David']['address web page']
. These three will access the associated data.
Since I believe this is the toughest part for a newcomer, I'll stop here since the rest should be easy. All you have to do is create new inputs in the correct if
conditions. Assign the captured input data into proper variables via the =
assignment operator (ex. email = input('Email: ')
), and then use the rest of the info logically. I hope this helps.