I have the following statement. I know it's very basic, but I can't figure out how to access the data inside the container in order to add it.
I wrote a program that stores the information related to patients: age, sex, ID, and if they are diabetic. Use a dictionary to record the provided data (you'll need to use a container for the values).
Ask the user for a DNI and increase the age of the corresponding patient by 1.
What I've done so far:
datos = {"Edad:":[60, 65, 45, 34, 58, 23],
"DNI:":[11412625, 6409217, 19172162, 28141815, 14972142, 36843316],
"Sexo:":["M", "F", "M", "F", "M", "F"],
"Es diabetico?":["Si", "No", "No", "Si", "Si", "No"]}
dni = input("ingrese dni: ")
for dni in datos:
for i in datos["DNI:"]:
datos.update({"Edad:"[i]:[i]+1})
print(datos)
Good day,
Since all the elements inside
datos
are lists, you could use the list's index() method to get the index of the "DNI" value entered into itinput
and then increment the age.Note: As "DNI" has numeric type values,
int
you must convert the valueinput
to an integer:complete example
Sample:
You can see that when entering the first "DNI" the value of the age with the same index increases by 1
To access any value in the dictionary, use the name of the dictionary and the name of the key, for example:
This would return all ages
If you want to access a specific age you can do it with the index number, for example the first age would be
Which would be the value
60
of the original dictionary (Of course, if you edit it, it would return another value)Therefore, to update the age value, we first look for the "DNI" index that contains the entered value,
input
which in the case of the example is0
because it is in the first position. We then use that value to access the same index but from the key "Age:" and update it.The first step in the solution is to find a suitable representation for the data.
In this problem, people data is handled. The data of a person can be represented with a dictionary where the keys are
edad
,sexo
,diabetico
.Since I have multiple people, I'll put them in a DIN-indexed dictionary.
Retrieving a person's data now requires no search. Simply use the DIN to find it:
produces: