uaveiro-leci/1ano/fp/aula06/school.py

49 lines
1.1 KiB
Python
Raw Normal View History

# Complete o programa!
# a)
def loadFile(fname, lst):
with open(fname, 'r') as f:
2023-01-30 16:48:43 +00:00
f.readline()
for line in f:
2023-01-30 16:48:43 +00:00
data = line.strip('\n').split('\t')
dataTuple = (int(data[0]), data[1], float(data[5]), float(data[6]), float(data[7]))
lst.append(dataTuple)
# b) Crie a função notaFinal aqui...
def notaFinal(reg):
return sum(reg[2:]) / 3
# c) Crie a função printPauta aqui...
# 4. Alterar a função...
def printPauta(lst, filename=""):
text = f'{"Numero":>6} {"Nome":^50} {"Nota":>4}\n'
for aluno in lst:
text += f'{aluno[0]:>6} {aluno[1]:^50} {notaFinal(aluno):>4.1f}\n'
2023-01-30 16:48:43 +00:00
2022-11-16 15:08:11 +00:00
print(text)
with open(filename, 'w') as f:
f.write(text)
# d)
def main():
lst = []
# ler os ficheiros (encontram-se na pasta 'datafiles' (~/1ano/fp/aula06/datafiles)
loadFile("datafiles/school1.csv", lst)
loadFile("datafiles/school2.csv", lst)
loadFile("datafiles/school3.csv", lst)
# ordenar a lista
lst.sort()
# mostrar a pauta
printPauta(lst, filename="datafiles/schooldata.txt")
# Call main function
if __name__ == "__main__":
main()