简体   繁体   中英

Add to values in a list inside a dictionary with append

I'm trying to create a friend dictionary in which I can add a friend and put there his info.

I want to use the friend's name as the key, two numbers, two emails and information about where he lives.

My problem is that my program crashes when asking for the numbers and for the emails, I dont know what i did wrong.

I used the append, function because the numbers of each friend are saved in a list. I dont want a new program I want to repair mine so I can understand why it's failing.

The other thing I'm trying to do is to not print the empty dictionary that im creating at the end, its a list with dictionaris in it (each friend is a dictionary), so i guess i should say to print the list from the position 1 to the end but i guess there is a better way, here I post my code, the error is when i ask for the first and second phone and mail.

def add_contact(friends):
    contact = {}
    contact["name"]=input("name: ")
    for i in range(2):
        contact["phone"][i]=input("phone: ") #Here it crashes
    for i in range(2):
        contact["mail"][i]=input("mail: ") #Here too

    contact["street"]=input("street: ")
    contact["housenum"]=input("housenum: ")
    contact["cp"]=input("cp: ")
    contact["city"]=input("city: ")
    friends.append(contact)

friends = [{"name":[{"telf":[0]*2},{"mail":[0]*2}, 
{"street":"","housenum":"","cp":"", "city":""}]}] #This is the list im creating to fill it with friends information, the first dictionary in the list is an empty dictionary which i dont want to print.
add_contact(friends)
print(friends)

You need to create a list for the phone and email, then append to it:

def add_contact(friends):
    contact = {}
    contact["name"]=input("name: ")
    contact["phone"] = []
    contact["mail"] = []
    for i in range(2):
        contact["phone"].append(input("phone: "))
    for i in range(2):
        contact["mail"].append(input("mail: "))

    contact["street"]=input("street: ")
    contact["housenum"]=input("housenum: ")
    contact["cp"]=input("cp: ")
    contact["city"]=input("city: ")
    friends.append(contact)

friends = [{"name":[{"telf":[0]*2},{"mail":[0]*2}, 
{"street":"","housenum":"","cp":"", "city":""}]}] #This is the list im creating to fill it with friends information, the first dictionary in the list is an empty dictionary which i dont want to print.
add_contact(friends)
print(friends)

The problem with your solution is that you try to add value to something which is not present.

When you do contact["phone"]. This creates a key inside dictionary contact. {"Phone":} But the problem is you do contact["phone"][i]. So ith element is searched in this key. Which is not present. Hence you get error. So you first need to add list to this dictionary. Then only you can add multiple numbers

def add_contact(friends):
    contact = {}
    contact["name"]=input("name: ")
    contact["phone"] = []
    contact["mail"] = []
    for i in range(2):
        contact["phone"].append(input("phone: "))
    for i in range(2):
        contact["mail"].append(input("mail: "))

    contact["street"]=input("street: ")
    contact["housenum"]=input("housenum: ")
    contact["cp"]=input("cp: ")
    contact["city"]=input("city: ")
    friends.append(contact)

friends = [{"name":[{"telf":[0]*2},{"mail":[0]*2}, 
{"street":"","housenum":"","cp":"", "city":""}]}] #This is the list im creating to fill it with friends information, the first dictionary in the list is an empty dictionary which i dont want to print.
add_contact(friends)
print(friends)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM