########################## BEGIN FUNCTION DEFINITIONS ######################
def view_all_contacts():
print(f"one's phone number: {contacts['one']}")
print(f"two's phone number: {contacts['two']}")
if len(contacts) == 0:
print("Your contacts are empty!")
return
def view_contact():
for name, number in contacts.items():
print(f"{name}'s phone number: {number}")
def add_contact():
name = input("Enter contact name: ")
number = input("Enter contact number: ")
if name == contacts:
print(f"{name} is already in your contacts!")
return
def update_contact():
contacts.append(name)
contacts[name] = number
print(f"Added {name} to your contacts")
def remove_contact():
print(f"{contacts}")
index = input(print("Enter the index of contact you want to remove from your contacts. "))
removed_contact = contacts.pop(index)
print(f"Removed {removed_contact} from your contacts. ")
########################## END FUNCTION DEFINITIONS ######################
options = """
(1) View All Contacts (2) View Contact (3) Add Contact
(4) Update Contact (5) Remove Contact (6) Exit
"""
contacts = {
"one": "111-111-1111",
"two": "222-222-2222",
}
while True:
choice = int(input(options))
if choice == 1:
view_all_contacts()
elif choice == 2:
view_contact()
elif choice == 3:
name = input("Enter contact name: ")
number = input("Enter contact number: ")
if name == contacts:
print(f"{name} is already in your contacts!")
elif choice == 4:
update_contact()
elif choice == 5:
remove_contact()
elif choice == 6:
break
else:
print(f"{choice} is not a valid option")
#현재 만들고 있는 코딩이 있습니다. 학원 숙제 같은 느낌이고 너무 어려워 도움을 좀 청하고 싶습니다.
#어떻게 해야 view_all_contacts()에 모든 추가한 연락처가 나올 수 있게 하는지
#즉 연락처를 추가하고 update_contact()를 작동 시켰을때 어떻게 해야 업데이트가 되게 하는지
#remove_contact()에서 어떻게 해야 연락처를 지울 수 있는지가 궁급합니다.
설명:
view_contact()
- Ask the user for the contact name
- If the name is not in the contacts
- Print a message saying the name is not in the contacts and exit the function.
- If the name is in the contacts
- Print a message like "Sarah's number is 398-394-1111"
update_contact()
- Ask the user for the contact name
- Ask the user for the contact number
- If the name is not in the contacts
- Print a message saying the name is not in the contacts and exit the function.
- If the name is in the contacts
- Update the contact name with the new number
*******************************************************************************
See the slide on using dict.pop() to remove an item before trying to
implement remove_contact(). It's in the homework section of the slides.
*******************************************************************************
remove_contact()
- Ask the user to enter the name to remove
- If the name is not in the contacts
- Print a message saying the name is not in the contacts and exit the function.
- If the name is in the contacts
- Remove the name from the contacts
- Print a message saying you removed the name from the contacts
Example Output:
-------------------------------------------------------------------------------
(1) View All Contacts (2) View Contact (3) Add Contact
(4) Update Contact (5) Remove Contact (6) Exit
1
Steve's phone number: 823-111-2384
Kim's phone number: 823-843-2435
(1) View All Contacts (2) View Contact (3) Add Contact
(4) Update Contact (5) Remove Contact (6) Exit
2
What is the name of the contact? Steve
Steve's number is 823-111-2384
(1) View All Contacts (2) View Contact (3) Add Contact
(4) Update Contact (5) Remove Contact (6) Exit
4
Enter contact name: Steve
Enter contact number: 222-333-4444
(1) View All Contacts (2) View Contact (3) Add Contact
(4) Update Contact (5) Remove Contact (6) Exit
5
Enter contact name: Steve
Removed Steve from your contacts.
(1) View All Contacts (2) View Contact (3) Add Contact
(4) Update Contact (5) Remove Contact (6) Exit
1
Kim's phone number: 823-843-2435
(1) View All Contacts (2) View Contact (3) Add Contact
(4) Update Contact (5) Remove Contact (6) Exit
6
-------------------------------------------------------------------------------
성적이나 상업적이용에 사용되는 것이 아닙니다.
취미활동으로 시작한 코딩이고 막혀서 도움을 요청하는 것입니다.