简体   繁体   English

关于 Python 上的方法和 2d arrays 的问题

[英]Problem regarding means and 2d arrays on Python

this is my first ever post and I'm a non-native english speaker so please try to be patient to me!这是我第一次发帖,而且我是非英语母语人士,所以请耐心等待我!

I'm having a issue regarding the use of 2d arrays on Python.我在 Python 上使用 2d arrays 时遇到问题。 The thing is that I need a program able to receive student's names and their grades for a set number of subjects, student's names must be saved to an 1d array and their grades must be saved to another 1d array.问题是我需要一个程序能够接收学生的姓名和他们对一组科目的成绩,学生的姓名必须保存到一维数组中,他们的成绩必须保存到另一个一维数组中。 Also the number of students and the name of each subject as their number must be issued by the user.此外,学生人数和每个科目的名称必须由用户发出。 My problem is that when it's the time to inform the user of the mean grade for each student and the mean grade for each subject the names don't add up with the resulting mean (I mean the resulting numbers are correct but they don't belong to the user/subject shown in the output).我的问题是,当需要通知用户每个学生的平均成绩和每个科目的平均成绩时,名称并没有与结果平均值相加(我的意思是结果数字是正确的,但它们没有属于输出中显示的用户/主题)。 I was also told to not use any libraries other than statistics and those native to Python (it's a college assignment).我还被告知不要使用除统计数据和 Python 原生库(这是大学作业)以外的任何库。 I attach my code (it's in spanish, might translate it if needed) to anyone who is looking forward to help me.我附上我的代码(它是西班牙语,如果需要可以翻译它)给任何期待帮助我的人。 Thanks in advance!提前致谢!

    print("******Instituto Técnico de Contaduría*******")

m=int(input("Ingrese la cantidad de estudiantes de la carrera Técnico en Contaduría: "))
n=int(input("Ingrese la cantidad de asignaturas: "))

est=[None]*m
mat=[None]*n
notasfinales=[[0.0 for x in range(n)] for y in range (m)]

for i in range (m):
      est[i]=str(input("Digite el nombre completo del(la) estudiante: "))

for j in range(n):
      mat[j]=str(input("Digite el nombre de la asignatura: "))


for i in range(m):
      print("*****Digite las notas del estudiante " + est[i] + "*****")
      for j in range(n):
          notasfinales[i][j]=float(input("Digite las notas de la asignatura " + mat[j] + "\n "))


print("****Promedio de notas por estudiante******")
for i in range(m):
    sumaFil=0
    for j in range(n):
        sumaFil= sumaFil + notasfinales[i][j]


    for i in range(m):
        pe=sumaFil/n

    print("El promedio del(la) estudiante " + est[i]  + " es " + str(pe))

print("****Promedio de notas por asignatura******")
for j in range(n):
    sumaCol=0
    for i in range(m):
        sumaCol= sumaCol + notasfinales[i][j]

    for j in range(n):
        pa=sumaCol/m

    print("El promedio de la asignatura " + mat[j]  + " es " + str(pa))

You are re-using and overwriting the same variable name in your final code section.您在最终代码部分中重复使用和覆盖相同的变量名称。 I highlight the issues here:我在这里强调问题:

print("****Promedio de notas por estudiante******")
for i in range(m): # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< first usage of i
    sumaFil=0
    for j in range(n):
        sumaFil= sumaFil + notasfinales[i][j]

    for i in range(m): # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< overwrite i
        pe=sumaFil/n

    print("El promedio del(la) estudiante " + est[i]  + " es " + str(pe))

print("****Promedio de notas por asignatura******")
for j in range(n): # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< first usage of j
    sumaCol=0
    for i in range(m):
        sumaCol= sumaCol + notasfinales[i][j]

    for j in range(n): # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< overwrite j
        pa=sumaCol/m

    print("El promedio de la asignatura " + mat[j]  + " es " + str(pa))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM