Edit Contact
Part of the Logic & Flow section of Coddy's Python journey — lesson 20 of 78.
Challenge
EasyThe next step is to create the edit_contact function. This function will allow users to update the details of an existing contact in the Contact Book.
Your Task:
- Create a function named
edit_contactthat takes one argument:contact_book(a dictionary). - Get input for the contact's name that the user wants to edit.
- Check if the name exists in the
contact_book:- If it exists, prompt the user to input new values for the contact's phone, email, and address (in that order!).
- If the user provides no input (presses Enter), keep the current value for that field (in this case the input will be an empty string,
''). - Update the contact's information in the dictionary.
- Print:
"Contact updated successfully!".
- If the contact does not exist, print:
"Contact not found!".
Remember: Only read inputs for phone, email, and address if the contact exists in the contact book. If the contact is not found, print the error message immediately without trying to read additional inputs.
Expected Behavior:
For a contact_book containing:
{"Alice": {"phone": "123-456-7890", "email": "alice@example.com", "address": "123 Main St"}}If the user enters:
Alice
987-654-3210
456 Elm StThe updated contact_book should look like this:
{"Alice": {"phone": "987-654-3210", "email": "alice@example.com", "address": "456 Elm St"}}If the user enters a name that does not exist:
BobThe output should be:
Contact not found!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!")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