Advertisement
Guest User

Hi Ahmed

a guest
Apr 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. import sys
  2. from pathlib import Path
  3. # Potential extension: make contacts an object
  4.  
  5.  
  6. def get_filename():
  7.     filename = input("\nFilename: ")
  8.     if filename[-4:] != ".txt":
  9.         filename += ".txt"
  10.     return filename
  11.  
  12.  
  13. def get_existing_filename():
  14.     while True:
  15.         filename = get_filename()
  16.         file = Path(filename)
  17.         if file.is_file():
  18.             return filename
  19.         else:
  20.             print(f"'{filename}' does not exist in this directory.")
  21.  
  22.  
  23. def find_contact():
  24.     filename = get_existing_filename()
  25.     file = open(filename, "r")
  26.     contact_name = input("\nContact name: ").lower()
  27.     lines = file.read().splitlines()
  28.     found = False
  29.     for line in lines:
  30.         if contact_name in line:
  31.             print(f"{contact_name.title()}: {line[line.index(':') + 1:]}")
  32.             found = True
  33.             break
  34.     if not found:
  35.         print(f"'{contact_name.title()}' was not found in the contact book.")
  36.     file.close()
  37.     menu()
  38.  
  39.  
  40. def create_contact():
  41.     while True:
  42.         print("\nCreate contact\n1) Add to existing contact book\n2) Create new contact book\n")
  43.         try:
  44.             option = int(input("Option: "))
  45.             if option == 1:
  46.                 filename = get_existing_filename()
  47.                 file = open(filename, "a")
  48.                 contact_name = input("\nContact name: ").lower()
  49.                 while True:
  50.                     try:
  51.                         phone_number = input("\nPhone number: ")
  52.                         int(phone_number)
  53.                         break
  54.                     except ValueError:
  55.                         print("Please enter a valid phone number.")
  56.                 file.write(f"{contact_name}:{phone_number}\n")
  57.                 file.close()
  58.                 print(f"Added {contact_name.title()} to the contact book.")
  59.                 break
  60.             elif option == 2:
  61.                 while True:
  62.                     filename = get_filename()
  63.                     file = Path(filename)
  64.                     if file.is_file():
  65.                         print("Please enter a filename that does not exist.")
  66.                     else:
  67.                         break
  68.                 file = open(filename, "w+")
  69.                 file.close()
  70.                 print(f"Created a new contact book called '{filename}'.")
  71.                 break
  72.             else:
  73.                 print("Please choose an option between 1 and 2.")
  74.         except ValueError:
  75.             print("Please choose an option between 1 and 2.")
  76.     menu()
  77.  
  78.  
  79. def menu():
  80.     while True:
  81.         print("\nMenu\n1) Find contact\n2) Create contact\n3) Quit\n")
  82.         try:
  83.             option = int(input("Option: "))
  84.             if option == 1:
  85.                 find_contact()
  86.             elif option == 2:
  87.                 create_contact()
  88.             elif option == 3:
  89.                 sys.exit()
  90.             else:
  91.                 print("Please choose an option between 1 and 3.")
  92.         except ValueError:
  93.             print("Please choose an option between 1 and 3.")
  94.  
  95.  
  96. print("Contact Book")
  97. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement