How to create an accumulator

0

I have some classes that I want to display the number of approved students.

What I've Done

nota= a 

if nota >= 60 and nota < 100:     
   

else:

Exit passed 20 disapproved 50, but I have seventy students. How to do?

    
asked by anonymous 10.07.2018 / 12:06

3 answers

2

If you have a list with the student's grades in the classes, you only have to check how many values are greater than 60. You can do this with the len() function and filter the grades 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.

    
10.07.2018 / 13:41
1

There are a few different ways to do this, but as I believe you are getting started, the idea would be to visualize each stage of structure you use to understand it.

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

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 in your list and for each note check as needed

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

Depending on the result 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))
    
10.07.2018 / 13:12
0

It is possible for students to be inserted into a dictionary, and it makes it easier to see who the students are being disapproved of.

Follow this example below, here I've limited students to 5 and entered them manually but you can change them 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)}')
    
10.07.2018 / 16:57