Home > Blockchain >  How to edit and delete keys and values in python dict
How to edit and delete keys and values in python dict

Time:12-02

i have a little problem that needs solving, i have to write a program that saves contacts in a dict and be able to 1- add new contacts 2- delete contacts 3- edit contacts 4- list contacts 5- show contacts

i wrote a simple program that saves contacts into a dictionary but i have a problem with the rest and i could really user some help!!

here is my code:

contacts = {"Mohamed": {"name": "Mohamed Sayed", "number": "017624857447", "birthday": "24.11.1996", "address": "Ginnheim 60487"},
            "Ahmed": {"name": "Ahmed Sayed", "number": "0123456789", "birthday": "06.06.1995", "address": "India"}}

def add_contact():

    for _ in range(0, 1):
        contact = {}
        name = input("Enter the name: ")
        number = input("Enter the number: ")
        birthday = input("Enter the birthday")
        address = input("Enter the address")


        contact["name"] = name
        contact["number"] = number
        contact["birthday"] = birthday
        contact["address"] = address
        print(contact)
        contacts.update(contact)


add_contact()
print(contacts)



def del_contact():
    user_input = input("Please enter the name of the contact you want to delete: ")
    for k in contacts:
        if user_input == contacts["name"]:
            del contacts[k]
del_contact()
print(contacts)


def edit_contact():
    user_input = input("please enter the contact you want to edit: ")
    for k, v in contacts:
        if user_input == contacts["name"]:
            contacts.update(user_input)


def list_contact():
    pass



def show_contact():
    user_input = input("please enter the contact you want to show: ")
    for k, v in contacts.items():
        if user_input == contacts["name"]:
            print(key, value)

show_contact()

CodePudding user response:

For your def's you dont need to use a for ... in range(...) loop, rather you can just call upon that value by the value of user_value. I've decided to not include def edit_contact(): in this as it currently doesn't edit anything, all it does is add a new element within contacts with that in mind

contacts = {"Mohamed": {"name": "Mohamed Sayed", "number": "017624857447", "birthday": "24.11.1996", "address": "Ginnheim 60487"},
            "Ahmed": {"name": "Ahmed Sayed", "number": "0123456789", "birthday": "06.06.1995", "address": "India"}}

def add_contact():
    for _ in range(0, 1):
        contact = {}
        name = input("Enter the name: ")
        number = input("Enter the number: ")
        birthday = input("Enter the birthday")
        address = input("Enter the address")
        contact["name"] = name
        contact["number"] = number
        contact["birthday"] = birthday
        contact["address"] = address
        print(contact)
        contacts[name] = contact
add_contact()
print(contacts)



def del_contact(contacts):
    user_input = input("Please enter the name of the contact you want to delete: ")
    contacts = contacts.pop(user_input)
del_contact(contacts)
print(contacts)


def edit_contact():
    user_input = input("please enter the contact you want to edit: ")
    for k, v in contacts:
        if user_input == contacts["name"]:
            contacts.update(user_input)


def list_contact():
    pass

def show_contact():
    user_input = input("please enter the contact you want to show: ")
    print(contacts[user_input])
    #you can use 'contacts[user_input]['name', 'number', 'birthday' or 'address']' to call upon specific elements within the list

show_contact()

Hope this helps.

  • Related