May-18-2017, 04:51 AM
(This post was last modified: May-18-2017, 05:40 AM by Liquid_Ocelot.)
(May-18-2017, 04:37 AM)Larz60+ Wrote: It looks as if names is a list.Yeah names is part of a list that has been put into a dictionary (thanks to your previous post xD)
try print(names) to see.
if so, you can find the index of the one you want by using index
idx = nicknames.index('nick') del nicknames[idx]not tested, but should be OK
So I have written
def deletecontact(): delname = input('Who would you like to delete (Enter nickname):') for nick in nicknames: if delname in nicknames: reply = input("Delete Contact? "+str(Names)+"? [y/[n]] ") if delname not in nicknames: print ("This Contact Does Not Exist!!\n----------------------\n") if reply=='y': idx = nicknames.index(delname) del nicknames[idx] print('Contact has been DELETED') menu()That seems to have done it using the index method :)
Now I just need to print a message to tell the user that there are no contacts listed if they go to show contacts and nothing is in the list because if I do that now I get this error
print ("--------------------" + "\n" + str(nicknames[i]) + "\n" + str(Names[i]) + "\n" + str(Numbers[i]) + "\n" + str(address[i]) + "\n" + "--------------------") IndexError: list index out of range
Not too sure but I may have figured it out and I really do apologize if I butcher this explanation
So I can save contacts just fine and print them that is cool but when I go to delete a contact (by searching its nickname) then again call the function to show the list of contacts it throws up the
IndexError: list index out of rangeTo explain this here is the input and output:
import sys Numbers=[] Names=[] nicknames=[] address=[] from collections import defaultdict x = [Numbers, Names, nicknames, address] len_dict = defaultdict(list) for word in x: len_dict[len(word)].append(word) len_dict def addcontact(): print ("-------------") print ("Adding a Contact\n-------------") name = input("Enter first and last name :") Names.append(name) nickname = input("Enter the nickname") nicknames.append(nickname) number = input("Enter number :") Numbers.append(number) Addres = input("Enter the address") address.append(Addres) print("***Contact added successfully***") menu() def searchcontact(): print ("\nSearch Contact by Nickname\n--------------------") nick = input("Please enter the nicnkname") for nick in nicknames: 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]) if nick not in nicknames: print ("\nThis Contact Does Not Exist!!\n----------------------\n") menu() def showcontact(): for i in range(len(Names)): print ("--------------------" + "\n" + str(nicknames[i]) + "\n" + str(Names[i]) + "\n" + str(Numbers[i]) + "\n" + str(address[i]) + "\n" + "--------------------") menu() def deletecontact(): delname = input('Who would you like to delete (Enter nickname):') for nick in nicknames: if delname in nicknames: reply = input("Delete Contact? "+str(Names)+"? [y/[n]] ") if delname not in nicknames: print ("This Contact Does Not Exist!!\n----------------------\n") if reply=='y': idx = nicknames.index(delname) del nicknames[idx] print('Contact has been DELETED') menu() def quit(): print("Exiting....") sys.exit def menu(): print ("---------------------------------------") print ("***Welcome to the Python address book***\n"+ "\n----------------------------------------"+ "\n| 1 | Add Contact"+ "\n| 2 | To search for the contact by nickname\n| 3 | To list all contacts in the address book\n| 4 | Delete Contact\n| 5 | 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': deletecontact() elif selection == '5': quit() else: print ("Unknown Command") menu() selection = input("Please Select:")And then the output:
--------------------------------------- ***Welcome to the Python address book*** ---------------------------------------- | 1 | Add Contact | 2 | To search for the contact by nickname | 3 | To list all contacts in the address book | 4 | Delete Contact | 5 | Quit ---------------------------------------- Please Select :1 ------------- Adding a Contact ------------- Enter first and last name :John Doe Enter the nicknameJohnny Enter number :333-000-000 Enter the address55 Hello Street ***Contact added successfully*** --------------------------------------- ***Welcome to the Python address book*** ---------------------------------------- | 1 | Add Contact | 2 | To search for the contact by nickname | 3 | To list all contacts in the address book | 4 | Delete Contact | 5 | Quit ---------------------------------------- Please Select :1 ------------- Adding a Contact ------------- Enter first and last name :Tom Hanks Enter the nicknameHanky Enter number :321-000-000 Enter the address123 Red Street ***Contact added successfully*** --------------------------------------- ***Welcome to the Python address book*** ---------------------------------------- | 1 | Add Contact | 2 | To search for the contact by nickname | 3 | To list all contacts in the address book | 4 | Delete Contact | 5 | Quit ---------------------------------------- Please Select :4 Who would you like to delete (Enter nickname):Hanky Delete Contact? ['John Doe', 'Tom Hanks']? [y/[n]] y Contact has been DELETED --------------------------------------- ***Welcome to the Python address book*** ---------------------------------------- | 1 | Add Contact | 2 | To search for the contact by nickname | 3 | To list all contacts in the address book | 4 | Delete Contact | 5 | Quit ---------------------------------------- Please Select :3 -------------------- Johnny John Doe Traceback (most recent call last): 333-000-000 File "C:/Users/LiquidO/Desktop/Semester 1 I.T/Python/a2/a3q2test.py", line 81, in <module> 55 Hello Street elif selection =='3': showcontact() -------------------- File "C:/Users/LiquidO/Desktop/Semester 1 I.T/Python/a2/a3q2test.py", line 47, in showcontact print ("--------------------" + "\n" + str(nicknames[i]) + "\n" + str(Names[i]) + "\n" + str(Numbers[i]) + "\n" + str(address[i]) + "\n" + "--------------------") IndexError: list index out of range Process finished with exit code 1