我创建了一个模拟学生笔记标点符号的代码,我的问题是是否可以在字典中遍历字典。也就是说,在所有用户 (1,2,3) 中浏览他们的笔记,然后得到一个平均笔记。我能够做到,但方式不同。这个想法可能是用一个 for 来做?我不知道是否可以这样做
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()))
您可以尝试像这样迭代:
您可以做很多事情来改进该代码。首先,数据结构。没有必要给每个学生贴上数字标签。此外,可以将成绩指定为一个列表,这样很容易获得平均值,因为对于一个成绩列表,平均值将为
sum(lista_notas) / len(lista_notas)
。那么最好在表单中指定数据结构:因此,您有一个学生列表,其中每个学生都是一本字典(您必须将字典理解为一种数据形式)。这样,每个学生在列表中都有一个位置 id,第一个学生的笔记将在表单中可用
students[0]['notas']
。因此,如果我想要学生的成绩n,我只需要调整之前的指令:现在很容易将平均值附加到列表中,然后计算有多少通过或失败。