How to create a accumulator

Asked

Viewed 353 times

0

I have a few classes I want to display number of approved students.

What I’ve already done

nota= a 

if nota >= 60 and nota < 100:     
   

else:

Exit approved 20 failed 50, but I have seventy students. How to do?

3 answers

2

If you have a list of students' grades in the classes, just check how many values are greater than 60. You can do this with the function len() and filter notes using list comprehension:

notas = [60, 75, 55, 86, 97, 90, 59]
qtd_aprovados = len([nota for nota in notas if nota >= 60])

print(f'Foram aprovados {qtd_aprovados} alunos')

For this example, it will appear that 5 students have been approved.

1

There are a few different ways of doing this, but as I think you’re getting started, the idea would be to visualize each step of structure used to understand.

An initial way would be to have a list, or some other data structure to have all the notes. Using list would be something like this

notas = [95, 60, 50, 30, 20, 10]

Then create two variables to store the totals for approved and disapproved, which can be done as follows

total_aprovados = 0, total_reprovados = 0

Use an iterative structure so that you can retrieve and identify each element of your list and for each note check according to the necessary condition

for nota in notas:
    if nota >= 60 and nota < 100:
    else:

Depending on the outcome of these conditionals increment the values of the variables

total_aprovados = total_aprovados + 1 ou total_reprovados = total_reprovados + 1

Putting it all together would be like this

notas = [95, 60, 50, 30, 20, 10]
total_aprovados = 0
total_reprovados = 0

for nota in notas:
    if nota >= 60 and nota < 100:
        total_aprovados = total_aprovados + 1
    else:
        total_reprovados = total_reprovados + 1

print('Total aprovados = {0}, Total reprovados = {1}'.format(total_aprovados, total_reprovados))

0

There is the possibility that if students are inserted in a dictionary, and facilitates the consultation of who are the students failed.

Follow this example below, here I limited the students up to 5 and entered them manually but you can change to your need without problems.

alunos = {}
aprovados = []
reprovados = []

i = 0
qtd = 5

while i < qtd:
    aluno = str(input('Digite o nome do Aluno: '))
    nota = float(input('Digite a nota do Aluno: '))

    alunos[aluno]=nota
    i += 1

    if nota >= 60 and nota < 100:
        aprovados.append(aluno)

    else:
        reprovados.append(aluno)

print(alunos)
print(f'Os alunos aprovados foram {aprovados} e são um total de {len(aprovados)}')
print(f'Os alunos aprovados foram {reprovados} e são um total de {len(reprovados)}')
  • Tip: the condition nota >= 60 and nota < 100 can be better written as 60 <= nota <= 100 (equal to 100 as well, as it is a valid note)

  • yes becomes more readable the way you quoted, I would still put the approved averages as variables to facilitate an eventual exchange approval parameters

Browser other questions tagged

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