List All
Part of the Logic & Flow section of Coddy's Python journey — lesson 22 of 78.
Challenge
EasyThe next step is to create the list_all_contacts function. This function will allow users to view all the contacts stored in the Contact Book along with their details.
Your Task:
- Create a function named
list_all_contactsthat takes one argument:contact_book(a dictionary). - Check if the
contact_bookis empty:- If it is empty, print:
"No contacts available.".
- If it is empty, print:
- If it is not empty:
- Loop through each contact in the dictionary and print their name, phone, email, and address in a readable format.
Expected Behavior:
For a contact_book containing:
{
"Alice": {"phone": "123-456-7890", "email": "alice@example.com", "address": "123 Main St"},
"Bob": {"phone": "234-567-8901", "email": "bob@example.com", "address": "456 Oak Ave"}
}
The output should be:
Name: Alice
Phone: 123-456-7890
Email: alice@example.com
Address: 123 Main St
Name: Bob
Phone: 234-567-8901
Email: bob@example.com
Address: 456 Oak AveTry 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!")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 Sorter