Answer to Question #190891 in Python for Fatine

Question #190891

write a tkinter gui program in python that collects contact details from the user. Contact details refer to the contact’s full name (first name and last name), phone number, date of birth and a category that the contact belongs to (Friends, Family, Work, Other)

The Phone Book will display the following Menu and will support the corresponding functionality: **************** Phone book Menu******************

Enter your choice:

1: Add a new contact

2: Remove an existing contact

3: Look up a contact

4: Update a contact phone number

5: Display all contacts

6: Delete all contacts

7: Exit phonebook


1
Expert's answer
2021-05-09T05:44:23-0400
import re
#addData function takes file name add data to it
def AddData(fileName):
    try:
        global uniqueID#unieque ID data to save in file
      
        regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'#regex pattern for email id
        fname="12"
        lname="12"
        phone="t"
        email="12"
        while fname.isnumeric():#if name have numeric value keep asking
            fname=input("enter first Name:")
        while lname.isnumeric():#if name have numeric value keep asking
            lname=input("enter last Name:")
        #if length <10
        #if phone do not start with 04
        #if phone alpha numeric keep asking
        while not phone.isnumeric() or len(phone)<10 or not phone.startswith("04"):
            phone=input("Enter phone number: ")      
        #use regex for email
        while not re.search(regex,email):
            email=input("enter email address: ")
        file_object = open(fileName, 'a')#open file in append mode
        #create a string string of data
        data=str(uniqueID)+" . "+fname+" "+lname+" "+phone+" "+email+"\n"
        file_object.write(data)#write data to file
        file_object.close()#close file object
        uniqueID+=1#increment unique id by 1
        print("Data Added successfully!")#prompt user for successfull message
    except FileNotFoundError:#throw exception
        pass
#view data function
def ViewData(fileName):
     fileObject = open(fileName, 'r') #open file in reading mode
            #create a list of all ines
     Lines = fileObject.readlines() #read all lines
            #run a loop in list
     for line in Lines:#get each lines
         if len(line)>10:#check for valid line
             List=line.split(' ')#split by space
             print(List[0],List[1]," Name: ",List[2],List[3]," phone: ",List[4])
             print("Email : ",List[5])#print data
             print()
            #close file
     fileObject.close()
   
def DeleteData(fileName,uniqueID):#file name to open and unique id to deletedata
    count=0#number of lines to track
    findFlag=False#flag unique id found or not
    read_file = open(fileName, "r")#open file in reading mode
    read_lines = read_file.readlines()#store all lines
    read_file.close()#close file object
  
    for line in read_lines:#traverse through each line
        List=line.rstrip().split(' ')#remove new line split line by space
        if List[0]==str(uniqueID):#if id found delete that line from list
            del read_lines[count]
            findFlag=True#set flag to true
        count+=1#increment line number
  
    writeFile=open(fileName,'w')#open file in writing mode
    #write each line to file
    for line in read_lines:
        writeFile.write(line+"\n")
  
    writeFile.close()#close file object
    #print message for success or not found
    if findFlag:
        print("Data Deleted successfully!")
    else:
        print("unique id data not found .")
  
  

if __name__ == "__main__":
    uniqueID=1001#unique id
    print("......................Welcome.......................")
    while True:
        #print menu
        print("Press number to select option.")
        print("1. View Details")
        print("2. ADD Details")
        print("3. Delete Details")
        print("4. exit program")
        #get choice
        try:
            choice=int(input("Enter your choice: "))#prompt to user
            if choice==1:#view data
                ViewData("contact.txt")
            elif choice==2:#add data
                AddData("contact.txt")
            elif choice==3:#get unique id
                ID=int(input("Enter unique ID to delete: "))
                DeleteData("contact.txt",ID)#delete id
            elif choice==4:#exit message
                print(" good bye. ")
                break
            else:#invalid choice
                print("Please enter valid choice.")
        except ValueError:
            pass

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS