Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from pathlib import Path
- def check_for_file():
- while True:
- filename = input("\nFilename: ")
- if filename[-4:] != ".txt":
- filename += ".txt"
- file = Path(filename)
- if file.is_file():
- return filename
- else:
- print(f"'{filename}' does not exist in this directory.")
- def find_contact():
- filename = check_for_file()
- file = open(filename, "r")
- contact_name = input("\nContact name: ").lower()
- lines = file.read().splitlines()
- for index, line in enumerate(lines):
- if contact_name in line:
- print(f"{contact_name.title()} found at index {index}.")
- file.close()
- menu()
- def create_contact():
- while True:
- print("\nCreate contact\n1) Add to existing contact book\n2) Create new contact book\n")
- try:
- option = int(input("Option: "))
- if option == 1:
- check_for_file()
- break
- elif option == 2:
- # create new file
- break
- else:
- print("Please choose an option between 1 and 2.")
- except ValueError:
- print("Please choose an option between 1 and 2.")
- print("We're out!")
- menu()
- def menu():
- while True:
- print("\nMenu\n1) Find contact\n2) Create contact\n3) Quit\n")
- try:
- option = int(input("Option: "))
- if option == 1:
- find_contact()
- elif option == 2:
- create_contact()
- elif option == 3:
- sys.exit()
- else:
- print("Please choose an option between 1 and 3.")
- except ValueError:
- print("Please choose an option between 1 and 3.")
- print("Contact Book")
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement