I am trying to get the delete function in this program to work, I have tried using '.pop', '.clear' and 'del' functions but don't seem to work with the addressbook technique i've gone with. I've looked for a while and tried all kinds. Anyone help with this?
addressBook = []
def find(name):
for i in addressBook:
if i['nickname'] == name:
return i
else:
continue
def add():
contact = {}
print("Enter 'cancel' to return to menu")
nickname = str.lower(input('Enter the nickname: '))
for i in addressBook:
if i['nickname'] == nickname:
print("Contact already exists!")
else:
continue
if nickname.lower() == 'cancel':
return None
else:
name = str.lower(input('Enter the full name: '))
if name.lower() == 'cancel':
return None
address = str.lower(input('Enter the address: '))
if address.lower() == 'cancel':
return None
phone = str.lower(input('Enter the phone number: '))
if phone.lower() == 'cancel':
return None
contact['nickname'] = nickname
contact['name'] = name
contact['address'] = address
contact['phone'] = phone
return contact
def delete(name): #prompt for a nickname, then find and display the related entry. Ask if this is correct to delete
for contact in addressBook:
if contact['nickname'] == name:
print(contact)
addressBook[contact]
print("Contact deleted!")
else:
print("Contact not found!")
continue
def listAll():
count = 0
for i in addressBook:
count += 1
print(count,'\t', i, '\n')
if addressBook == []:
print("No contacts found!")
print("Welcome to your address book!")
print("***My Contacts***")
print("\t F = Find a contact")
print("\t A = Add new contact")
print("\t D = Delete a contact")
print("\t L = List all contacts")
print("\t Q = Quit")
while True:
try:
global command
command = str.upper(input("Command: ? "))
if command == 'F':
result = find(str.lower(input('Enter nickname to find: ')))
if result != None:
resultString = ''
for key,val in result.items():
resultString = resultString + str.title(key) + ': ' + str.title(val) + ' | '
print(resultString)
else:
print('Contact not found!')
elif command == 'A':
while True:
details = add()
if details == None:
break
else:
addressBook.append(details)
print("Contact added!")
elif command == 'D':
delete(str.lower(input('Enter nickname of contact to delete: ')))
elif command == 'L':
listAll()
elif command == 'Q':
exit()
break
else:
print("Invalid command, please try again!")
continue
except:
if command == 'Q':
exit()