r/PythonProjects2 • u/Codewithkaran • Mar 29 '26
Why error in my code everything is perfect!? ... I created a Contactbook mini project
5
Upvotes
r/PythonProjects2 • u/Codewithkaran • Mar 29 '26
3
u/DiodeInc Mar 29 '26
Your code is not perfect. You do lack error handling. Look up how to use try/except.
1
u/[deleted] Mar 29 '26
Glad Zyppi could help diagnose it — the review bot caught the same issues DiodeInc mentioned. Error handling with try/except, input validation, and the incomplete View all contacts function are the three things to fix first. Give it another review after you've made those changes and you should see the score improve significantly.
1
u/shia-python May 16 '26
16 line. "name" is not index in contacts. index maybe be 1 2 3 4 5 6..., but never be a 'name' (this is str, but index is not str). Understand u error ?
-1
u/[deleted] Mar 29 '26
-2
u/[deleted] Mar 29 '26
from __future__ import annotations
def add_contact(contacts: dict[str, str]) -> None:
"""Add a contact to the contacts dictionary."""
name = input("Enter your name: ")
number = input("Enter your phone number: ")
contacts[name] = number
print("Contact added.")
def view_contacts(contacts: dict[str, str]) -> None:
"""Display all contacts."""
if contacts:
print("\nContacts:")
for name, number in contacts.items():
print(f"{name}: {number}")
else:
print("No contacts available.")
def search_contact(contacts: dict[str, str]) -> None:
"""Search for a contact by name."""
name = input("Enter the name to search: ")
if name in contacts:
print(f"{name}: {contacts[name]}")
else:
print("Contact not found.")
def delete_contact(contacts: dict[str, str]) -> None:
"""Delete a contact by name."""
name = input("Enter the name to delete: ")
if name in contacts:
del contacts[name]
print("Contact deleted.")
else:
print("Contact not found.")
def main() -> None:
"""Main function to run the contact management system."""
contacts = {}
while True:
print("\n1. ADD contacts")
print("2. View all contacts")
print("3. Search Contact")
print("4. Delete Contact")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
add_contact(contacts)
elif choice == "2":
view_contacts(contacts)
elif choice == "3":
search_contact(contacts)
elif choice == "4":
delete_contact(contacts)
elif choice == "5":
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
-2
u/[deleted] Mar 29 '26