I want to take input from the user and initialize a list of dictionaries. I have the following block which works fine.
people = []
for p in range(3):
cell = {"name": "","age" : 0, "education" : "","height" : 0}
cell["name"] = input("name:")
cell["age"] = int(input("age:"))
cell["education"] = input("education:")
cell["height"] = float(input("height:"))
people.append(cell)
The problem I have is why the following block does not work for me.
people = []
cell = {"name": "","age" : 0, "education" : "","height" : 0}
for p in range(0,3):
cell["name"] = input("name:")
cell["age"] = int(input("age:"))
cell["education"] = input("education:")
cell["height"] = float(input("height:"))
people.append(cell)
I do not understand why at the end of the iteration I have the list initialized with the latest input all the 3 times, I mean when I use this line:
cell["name"] = input("name:")
shouldn't the previous value been replaced with the new one?