May-17-2017, 10:42 PM
So I am writing a code for an address book. I have a function that searches for a nickname of a person then prints their details if the nickname is in the list but I only getting the "contact does not exist" every time I run this Please help! also P.S does anyone have any good resources on turning lists into dictionaries? Cheers
import sys Numbers=[] Names=[] nicknames=[] address=[] def addcontact(): print ("-------------") print ("Adding a Contact\n-------------") name = input("Enter first and last name :").lower Names.append(name) nickname = input("Enter the nickname").lower nicknames.append(nickname) number = input("Enter number :") Numbers.append(number) Addres = input("Enter the address").lower address.append(Addres) print("***Contact added succesfully***") menu() def searchcontact(): print ("\nSearch Contact by Nickname\n--------------------") nick = input("Please enter the nicnkname") if nick in nicknames: print ("The Contacts are listed Below") print ("-----------------------------") index = nicknames.index(nick) print ("\n", nicknames[index]) print ("\n",Names[index]) print ("\n",address[index]) print ("\n", Numbers[index]) elif nick not in nicknames: print ("\nThis Contact Does Not Exist!!\n----------------------\n") menu() def showcontact(): for i in range(len(Names)): print (str(Names[i]) + "........"+ str(Numbers[i])) #print Names #print Numbers print ("--------------------") def quit(): print("Exiting....") sys.exit def menu(): print ("---------------------------------------") print ("***Welcome to the Python address book***\n"+ "\n----------------------------------------"+ "\n| 1 | To Add Contact"+ "\n| 2 | To search for the Contact by nickname\n| 3 | To list all contacts in the address book\n| 4 | To quit\n"+ "----------------------------------------\n>") menu() while True: selection = input("Please Select:") if selection =='1': addcontact() elif selection =='2': searchcontact() elif selection =='3': showcontact() elif selection == '4': quit() else: print ("Unknown Command") menu() selection = input("Please Select:")
--------------------------------------- ***Welcome to the Python address book*** ---------------------------------------- | 1 | To Add Contact | 2 | To search for the Contact by nickname | 3 | To list all contacts in the address book | 4 | To quit ---------------------------------------- > Please Select:1 ------------- Adding a Contact ------------- Enter first and last name :John Doe Enter the nicknameJohnny Enter number :123456789 Enter the address123 Hello World street ***Contact added succesfully*** --------------------------------------- ***Welcome to the Python address book*** ---------------------------------------- | 1 | To Add Contact | 2 | To search for the Contact by nickname | 3 | To list all contacts in the address book | 4 | To quit ---------------------------------------- > Please Select:2 Search Contact by Nickname -------------------- Please enter the nicnknameJohnny This Contact Does Not Exist!! ---------------------- --------------------------------------- ***Welcome to the Python address book*** ---------------------------------------- | 1 | To Add Contact | 2 | To search for the Contact by nickname | 3 | To list all contacts in the address book | 4 | To quit ---------------------------------------- > Please Select: