A function that takes the names of two files as arguments

Asked

Viewed 122 times

-2

Program containing a function that takes the names of two files as arguments. The first file contains student names and the second file contains student grades. In the first file, each line corresponds to a student’s name and in the second file, each line corresponds to the students' grades (one or more). Assume that the notes were stored as string, and are separated from each other by whitespace. Read both files and generate a third file that contains the student’s name followed by the average of their grades.

I started out like this:

lista= []

with open('arquivo1linha.txt', 'a') as file:
        novo = "\n"+input("ALUNO 1: ")
        novo += "\n"+input("ALUNO 2: ")
        novo += "\n"+input("ALUNO 3: ")
        file.write(novo)
      
with open('arquivo2linha.txt', 'a') as arquivo:
        novo = "\n"+input("NOTA 1: ")
        novo += "\n"+input("NOTA 2: ")
        novo += "\n"+input("NOTA 3: ")
        arquivo.write(novo)
     
file = open('arquivo1linha.txt', 'r')
file.readlines()
['primeira linha\n', 'segunda linha\n', 'terceira linha\n',]
file.readlines()
file.close()
   
arquivo = open('arquivo2linha.txt', 'r')
arquivo.readlines()
['primeira linha\n', 'segunda linha\n', 'terceira linha\n',]
arquivo.readlines()
arquivo.close()

linha_arquivo_3 = arquivo1linha[]+ " " + arquivo2linha[]
    print (linha_arquivo_3)


print("Número de linhas: ", len(file.read))
print("Número de linhas: ", len(arquivo.read))

I can’t go on

Help?

1 answer

0

This solves, I just added/rename some variaves, but the part of the code to generate the files are the same as the question

lista= []

def countAluno(fAluno = 'aluno.txt'):
    with open(fAluno) as f:
        i = 0
        for line in f:
            i += 1
    return i

with open('aluno.txt', 'a') as file:
        novo = input("ALUNO 1: ")
        novo += "\n"+input("ALUNO 2: ")
        novo += "\n"+input("ALUNO 3: ")
        file.write(novo)
        file.close()
        
fAluno = open('aluno.txt', 'r')
alunos = fAluno.readlines()
fAluno.close()
    
      
with open('nota.txt', 'a') as arquivo:
    for n in range(countAluno()):
        print(alunos[n])
        novo = input("NOTA 1: ")
        novo += " "+input("NOTA 2: ")
        novo += " "+input("NOTA 3: ") + "\n"
        arquivo.write(novo)
    arquivo.close()
        
fNota = open('nota.txt', 'r')
notas = fNota.readlines()
fNota.close()

def calculoMedia(arqAluno='aluno.txt',  arqNota= 'nota.txt'):
    
    fAluno = open(arqAluno, 'r')
    alunos = fAluno.readlines()
    fAluno.close()

    fNota = open(arqNota, 'r')
    notas = fNota.readlines()
    fNota.close()
    
    for n in range(countAluno()):
        aluno = alunos[n].replace('\n', '')
        nota = notas[n].replace('\n', '').split(' ')
        valor = [int(val) for val in nota]
        lista.append([aluno,valor])
        
    with open('media.txt', 'a') as arquivo:
        for n in range(len(lista)):
            aluno = lista[n][0]
            media = sum(lista[n][1])/3
            arquivo.write(aluno +' '+str(media)+'\n')
        arquivo.close()

calculoMedia()
  • Thank you very much. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.