0

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()
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
A.Jan
  • 3
  • 1
  • what do you try to achieve? what would be a simple input and a simple output? – Colonel Beauvel Jun 02 '16 at 10:11
  • a simple input would have a number of entries such as addressBook = [{'name': 'John Smith', 'nickname':'Johnny', 'address':'21st Street', 'Phone':'02555555'}{'name': 'Steve Nicks', 'nickname':'Stevo', 'address':'52nd Street', 'Phone':'02444444'}] And I would like to delete the contact that hold's "Steve's" contact. The output would then only print "John's" contact. Basically, how would I delete a contact from the list "addressBook" – A.Jan Jun 04 '16 at 08:28

0 Answers0