0

Hello i have a school exercise that i cant solve, im almost there but something is missing me, so i have 2 .txt files one with student names and other one with students grades they are like:

nomesAlunos.txt :
Ricardo
Filipa
Teresa
Claúdio


nomesNotas.txt :
14 15 14 17
14 14
13 10
13 10 11

i should create a new file where i have the student name followed by student average grades, my code is like:

final=open('data/notasFinal.txt','w')
nomes=open('data/nomesAlunos.txt')
notas=open('data/nomesNotas.txt')
for linha in nomes:
    final.write(linha)
    for line in notas:
        lista=line.split()
        media=(sum((int(numeros) for numeros in lista)))/(len(lista))
        final.write(str(media))
nomes.close()
notas.close()

my final .txt file is like this right now:

Ricardo
15.014.011.511.333333333333334Filipa
Teresa
Claúdio

SOLVED:

final=open('data/notasFinal.txt','w')
nomes=open('data/nomesAlunos.txt')
notas=open('data/nomesNotas.txt')
media=[]
i=0
for linhas in notas:
    lista=linhas.split()
    media.append((sum((int(numeros) for numeros in lista)))/(len(lista)))
for nome in nomes:
    alunos=nome.split()
    final.write(f"{alunos[0]:30}{media[i]}\n")
    i+=1
nomes.close()
notas.close()
final.close()
wwii
  • 23,232
  • 7
  • 37
  • 77
  • Hi, welcome to Stack Overflow. Exactly what is the problem you're having? – Spangen May 28 '18 at 13:15
  • i wanted something like: `Ricardo 15.0 Filipa 14.0 Teresa 11.5 Cláudio 11.33333333333333` – Gabriel Domingues May 28 '18 at 13:17
  • @wwii its not just that i must get the average of the grades. – Gabriel Domingues May 28 '18 at 15:21
  • Your expression for `media` looks correct: split the line; add the results (*cast* to ints) divide by length.. You just need to refactor for iterating over the files in parallel. I think the link I posted solves the problem you were having.. – wwii May 28 '18 at 15:26

3 Answers3

1

As to not give the full answer away (this is a school assignment after all!), instead of looping through your 'notas' file within the 'nomes' loop, you may want to consider using Python's notas.readline function on each iteration of your loop.

Presently, you're looping through every line of 'notas' once you hit the every time you try to loop through a line of 'nomes'. However, on the first loop, you have gone through the entire 'nomes,' file, and there is nothing left to read.

ollien
  • 4,418
  • 9
  • 35
  • 58
0

Your iterations are not set up such that they solve your problem.

The first loop iterates through the names. It will write the first name, then jump into the second loop. There it will look for all the grades of all the students. When it is done, it will continue the first loop, printing out the next name. Now it goes back into the loop for the grades, but this file has already been read to the end, so it does not print anything anymore.

Solution: You only need one loop. This loop will iterate over each number of line in BOTH files.

Even better though you can read each line seperately with next(nomes) and next(noats) and put THAT into your loop until there is nothing more to read.

offeltoffel
  • 2,691
  • 2
  • 21
  • 35
0

This will set you going, let know if you don't understand at any point:-

with open('out.txt','w') as outfile, open('names.txt','r') as in1, open('grades.txt','r') as in2:
    inp1 = in1.read().split("\n");
    inp2 = in2.read().split("\n");
    out = list(map(list, (zip(inp1,inp2))));
    print(out)
    out_text = ("\n").join([" ".join(i) for i in out])
    outfile.write(out_text)
Abhijeetk431
  • 847
  • 1
  • 8
  • 18
  • This will give the expected result, but will not contribute to understanding the concept of programming. Also, as a teacher, I would doubt the student came up with this on his/her own. So it will raise more questions than it answers. – offeltoffel May 28 '18 at 13:37
  • dw @offeltoffel im trying to do it by my own anyway, i didnt want to use zip in here – Gabriel Domingues May 28 '18 at 13:54
  • You are right Mr @offeltoffel – Abhijeetk431 May 28 '18 at 13:58
  • instead of zip @GabrielDomingues you can loop over the lists with indexing and get the same output :) `for i in range(len(inp1)):` `out_text = inp1[i]+" "+inp2[i]+"\n"`... something like this... – Abhijeetk431 May 28 '18 at 14:01