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
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