Everything Together
Part of the Logic & Flow section of Coddy's Python journey. Lesson 23 of 78.
Challenge
EasyNow that you've built all the individual functions for the Contact Book, it's time to put them together to create the full program!
Your Task:
- Combine the functions you've created:
display_menu: Displays the menu options for the user.add_contact: Adds a new contact to the Contact Book.view_contact: Displays details for a specific contact.edit_contact: Updates an existing contact's information.delete_contact: Removes a contact from the Contact Book.list_all_contacts: Displays all the contacts in the Contact Book.
- Create a dictionary called
contact_bookto store the contacts. - Write a loop that:
- Displays the menu using
display_menu. - Accepts user input for the menu choice.
- Calls the appropriate function based on the user's choice.
- Prints
Invalid choice. Please try again.if the user enters a choice that is not one of the valid menu options. - Continues until the user selects the option to exit the program.
- Displays the menu using
Expected Behavior:
When you run the program, it should:
- Show a menu of options for the user to choose from.
- Allow the user to interact with the Contact Book by calling the appropriate function.
- Print Invalid choice. Please try again. when the user enters an unrecognized option.
- Exit the program cleanly when the user selects the "Exit" option.
This final step combines all the knowledge you've gained into a fully functioning Contact Book application. Enjoy seeing your hard work come together! 🎉
Try it yourself
def display_menu():
print("Contact Book Menu:")
print("1. Add Contact")
print("2. View Contact")
print("3. Edit Contact")
print("4. Delete Contact")
print("5. List All Contacts")
print("6. Exit")
def add_contact(contact_book):
name = input()
if name in contact_book:
print("Contact already exists!")
return
phone = input()
email = input()
address = input()
contact_book[name] = {"phone": phone, "email": email, "address": address}
print("Contact added successfully!")
def view_contact(contact_book):
name = input()
if name in contact_book:
contact = contact_book[name]
print(f"Name: {name}")
print(f"Phone: {contact['phone']}")
print(f"Email: {contact['email']}")
print(f"Address: {contact['address']}")
else:
print("Contact not found!")
def edit_contact(contact_book):
name = input()
if name in contact_book:
phone = input()
email = input()
address = input()
if phone == '':
phone = contact_book[name]['phone']
if email == '':
email = contact_book[name]['email']
if address == '':
address = contact_book[name]['address']
contact_book[name] = {"phone": phone, "email": email, "address": address}
print("Contact updated successfully!")
else:
print("Contact not found!")
def delete_contact(contact_book):
name = input()
if name in contact_book:
del contact_book[name]
print("Contact deleted successfully!")
else:
print("Contact not found!")
def list_all_contacts(contact_book):
if not contact_book:
print("No contacts available.")
else:
for name, details in contact_book.items():
print(f"Name: {name}")
print(f"Phone: {details['phone']}")
print(f"Email: {details['email']}")
print(f"Address: {details['address']}")
print() # Blank line between contacts for readability
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary SorterPractice on your own: Online Python compiler