I created a code simulating the punctuation of notes to students and my question is if a dictionary can be traversed within a dictionary. that is, within all users (1,2,3) go through their notes and then get an average note. I was able to do it but in a different way. The idea is maybe to do it with a for? I have no idea if it can be done that way
students = {
"1" :{
"nombre": "Valentin",
"apellido": "Blanco",
"nota": " "
},
"2" :{
"nombre": "Lolo",
"apellido": "Gutierrez",
"nota": " "
},
"3" :{
"nombre": "Juanito",
"apellido": "Montana",
"nota": " "
}
}
contador = len(students.keys())
aprobados = list([])
suspendidos= list([])
stud = input("Ingrese el estudiante: ")
new_Nota = int(input("Ingrese la nota del estudiante: "))
acumulador = 0
while (contador > 0):
if (stud not in students.keys()):
print("Estudiante inexistente")
if (stud in students.keys()):
contador -= 1
acum += new_Nota
students[stud]["nota"] = new_Nota
if (new_Nota <= 7):
suspendidos.append([stud])
elif (new_Nota >= 6):
aprobados.append([stud])
if (contador == 0):
break
stud = input("Ingrese el estudiante: ")
new_Nota= int(input("Ingrese la nota del estudiante: "))
print("Aprobados:",aprobados)
print("Suspendidos:",suspendidos)
print("El promedio de las notas es ", acumulador/len(students.keys()))
You could try iterating like this:
There are many things you can do to improve that code. To begin with, the data structure. It is not necessary to label each student with a number. In addition, the grades can be specified as a list for which it will be easy to obtain the average since for a list of grades the average will be
sum(lista_notas) / len(lista_notas)
. Then it will be better to specify the data structure in the form:So you have a list of students, where each student is a dictionary (you have to understand a dictionary as a data form). In this way each student has a position id in the list and the notes of the first student will be available in the form
students[0]['notas']
. So if I want the student's grades n I will only have to adjust the previous instruction:Attaching averages to a list and then counting how many pass or fail is now easy.