Advertisement
Guest User

Ahmed is amazing

a guest
Apr 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import sys
  2. from pathlib import Path
  3.  
  4.  
  5. def check_for_file():
  6.     while True:
  7.         filename = input("\nFilename: ")
  8.         if filename[-4:] != ".txt":
  9.             filename += ".txt"
  10.         file = Path(filename)
  11.         if file.is_file():
  12.             return filename
  13.         else:
  14.             print(f"'{filename}' does not exist in this directory.")
  15.  
  16.  
  17. def find_contact():
  18.     filename = check_for_file()
  19.     file = open(filename, "r")
  20.     contact_name = input("\nContact name: ").lower()
  21.     lines = file.read().splitlines()
  22.     for index, line in enumerate(lines):
  23.         if contact_name in line:
  24.             print(f"{contact_name.title()} found at index {index}.")
  25.     file.close()
  26.     menu()
  27.  
  28.  
  29. def create_contact():
  30.     while True:
  31.         print("\nCreate contact\n1) Add to existing contact book\n2) Create new contact book\n")
  32.         try:
  33.             option = int(input("Option: "))
  34.             if option == 1:
  35.                 check_for_file()
  36.                 break
  37.             elif option == 2:
  38.                 # create new file
  39.                 break
  40.             else:
  41.                 print("Please choose an option between 1 and 2.")
  42.         except ValueError:
  43.             print("Please choose an option between 1 and 2.")
  44.     print("We're out!")
  45.     menu()
  46.  
  47.  
  48. def menu():
  49.     while True:
  50.         print("\nMenu\n1) Find contact\n2) Create contact\n3) Quit\n")
  51.         try:
  52.             option = int(input("Option: "))
  53.             if option == 1:
  54.                 find_contact()
  55.             elif option == 2:
  56.                 create_contact()
  57.             elif option == 3:
  58.                 sys.exit()
  59.             else:
  60.                 print("Please choose an option between 1 and 3.")
  61.         except ValueError:
  62.             print("Please choose an option between 1 and 3.")
  63.  
  64.  
  65. print("Contact Book")
  66. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement