View Contact
Part of the Logic & Flow section of Coddy's Python journey — lesson 19 of 78.
Challenge
MediumCreate a function named view_contact that displays details of a specific contact.
Your function should:
- Take a contact book dictionary as a parameter
- Get a contact name from user input (using
input()) - Display the contact's details if found
- Print "Contact not found!" if the contact doesn't exist
When displaying a contact, use this exact format:
Name: [name]
Phone: [phone]
Email: [email]
Address: [address]Example:
If the contact book contains Alice's information and the user enters "Alice", output:
Name: Alice
Phone: 123-456-7890
Email: alice@example.com
Address: 123 Main StIf the user enters "Bob" (who doesn't exist), output:
Contact not found!Note: Your function should only output the contact details or the error message - no additional prompting text.
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!")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