How to add values from within a Python list?

Asked

Viewed 72 times

-5

I made a code that stores data in a dictionary, and the dictionary is stored in a list, but when I try to add and divide the values, the first average is the one that ends up being valid for all students.

print('RU do aluno: - ')

alunos = []
notas = []
N = int(input('Qual o número de alunos? '))
for i in range(N):
    boletim = {'nome' : 0, 'n1' : 0, 'n2' : 0, 'n3' : 0, 'n4' : 0, 'media' :0}
    boletim['nome'] = input('Digite o nome do aluno: ')
    boletim['n1'] = float(input('Digite a primeira nota: '))
    boletim['n2'] = float(input('Digite a segunda nota: '))
    boletim['n3'] = float(input('Digite a terceira nota: '))
    boletim['n4'] = float(input('Digite a quarta nota: '))
    boletim ['media'] = boletim ['n1'], boletim ['n2'], boletim ['n3'], boletim ['n4']
    alunos.append(boletim)
    notas.append(boletim['media'])
    soma_das_notas = 0
    for nota in notas[0]:
        soma_das_notas += nota
    boletim['media'] = soma_das_notas / 4
    if boletim['media'] >= 7:
        boletim['media'] = 'aprovado'
    else:
        boletim['media'] = 'reprovado'
print('\nNota dos alunos')
print('-' * 90)
for aluno in alunos:
    print(f'{aluno["nome"]}       {aluno["n1"]}   {aluno["n2"]}   {aluno["n3"]}   {aluno["n4"]}   {aluno["media"]}')

He returns to me the following:

esse é o retorno, a nota da média do primeiro aluno cadastrado é a que fica para todos.

1 answer

1


Hello, friend. Well, the error is happening, more specifically because of line 17 of your code:

for nota in notas[0]:

You have set the calculation only for the first element of the notes list, element 0. Therefore, the average will only be calculated with the grades of the first student registered. A very simple option to solve the problem is to add a variable at the beginning of the code with a value of 0 and, at each loop repeat, add 1 to this variable, so that the index will track the number of students in the list.

In the code below, I chose the variable z to execute what I explained.

  print('RU do aluno: - ')

alunos = []
notas = []
z = 0 #Definindo a variável
N = int(input('Qual o número de alunos? '))
for i in range(N):
    boletim = {'nome' : 0, 'n1' : 0, 'n2' : 0, 'n3' : 0, 'n4' : 0, 'media' :0}
    boletim['nome'] = input('Digite o nome do aluno: ')
    boletim['n1'] = float(input('Digite a primeira nota: '))
    boletim['n2'] = float(input('Digite a segunda nota: '))
    boletim['n3'] = float(input('Digite a terceira nota: '))
    boletim['n4'] = float(input('Digite a quarta nota: '))
    boletim ['media'] = boletim ['n1'], boletim ['n2'], boletim ['n3'], boletim ['n4']
    alunos.append(boletim)
    notas.append(boletim['media'])
    soma_das_notas = 0
    for nota in notas[z]: #Garantindo que o índice acompanhará o número de alunos
        soma_das_notas += nota
    boletim['media'] = soma_das_notas / 4
    if boletim['media'] >= 7:
        boletim['media'] = 'aprovado'
    else:
        boletim['media'] = 'reprovado'

    z += 1
print('\nNota dos alunos')
print('-' * 90)
for aluno in alunos:
    print(f'{aluno["nome"]}       {aluno["n1"]}   {aluno["n2"]}   {aluno["n3"]}   {aluno["n4"]}   {aluno["media"]}')

These two small changes should solve the problem. Let me know if I can help you. Good luck!

  • Thank you so much, you helped me so much.

Browser other questions tagged

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