would like to make a contact book using dictinaries but i cant figure out how to add a nested dict to the current dict i have.
would be something like this
my_contacts = {"1": { "Tom Jones", "911", "22.10.1995"},
"2": { "Bob Marley", "0800838383", "22.10.1991"}
}
def add_contact():
user_input = int(input("please enter how many contacts you wanna add: "))
for i in range(user_input):
name = input("Enter the name: ")
number = input("Enter the number: ")
birthday = input("Enter the birthday")
adress = input("Enter the address")
my_contacts[name] = number
my_contacts[birthday] = adress
but unforunatly this doesnt add them as a dict. so how can i add them as one dict inside my current dict?
my_contacts = {"1": { "Tom Jones", "911", "22.10.1995"},
"2": { "Bob Marley", "0800838383", "22.10.1991"}
}
def add_contact():
user_input = int(input("please enter how many contacts you wanna add: "))
for i in range(user_input):
name = input("Enter the name: ")
number = input("Enter the number: ")
birthday = input("Enter the birthday")
adress = input("Enter the address")
my_contacts[name] = number
my_contacts[birthday] = adress
CodePudding user response:
So basically what you have here:
my_contacts = {"1": { "Tom Jones", "911", "22.10.1995"},
"2": { "Bob Marley", "0800838383", "22.10.1991"}
}
Is a dict of sets (mapping a string to a set), not a dict of dicts.
1.Pass the current contacts to your function (so you can add new contact, not create them from scratch)
2.Create a new dict for the new contact
3.Add it to the passed contacts dict
def add_contact(contacts: dict)->None:
user_input = int(input("please enter how many contacts you wanna add: "))
for i in range(user_input):
name = input("Enter the name: ")
number = input("Enter the number: ")
birthday = input("Enter the birthday")
address = input("Enter the address")
new_contact = {"name":name, "number":number, "birthday":birthday, "address":address}
next_key = str(max([int(k) for k in contacts.keys()]) 1)
contacts[new_key] = new_contact
CodePudding user response:
This will be a nice solution for you.
All i'm doing here is every time you iterate inside your for loop you are creating a new dictionary of the details you want to put in for a contact and adding that dictionary to the original dictionary, therefor adding a new contact as a dictionary. There should be some extra error handling of course but this for now may be a good solution.
my_contacts = {1: {"Name": "Tom Jones",
"Number": "911",
"Birthday": "22.10.1995",
"Address": "212 street"},
2: {"Name": "Bob Marley",
"Number": "0800838383",
"Birthday": "22.10.1991",
"Address": "31 street"}
}
def add_contact():
user_input = int(input("please enter how many contacts you wanna add: "))
index = len(my_contacts) 1
for _ in range(user_input):
details = {}
name = input("Enter the name: ")
number = input("Enter the number: ")
birthday = input("Enter the birthday")
address = input("Enter the address")
details["Name"] = name
details["Number"] = number
details["Birthday"] = birthday
details["Address"] = address
my_contacts[index] = details
index = 1