Storing variable values inside the while function

2

In one exercise I replied with this code:

qntd_alunos = int(input("Digite a quantidade de alunos: "))
qnt = 0
while qnt <= qntd_alunos-1:
    MB1 = float(input("Digite a média do primeiro bimestre: "))
    MB2 = float(input("Digite a média do segundo bimestre: "))
    media_semestral = (MB1 + MB2) / 2
    if media_semestral >= 7:
        print("Você foi aprovado")
        print("Sua média semestral é: ", media_semestral)
    else:
        print("Sua média semestral é: ", media_semestral)
    qnt = qnt+1
    if qnt == 1:
        print(qnt, "aluno já recebeu sua média, faltam", qntd_alunos - qnt)

    else:
        print(qnt, "alunos já receberam suas médias, faltam", qntd_alunos - qnt)

This code calculates the half-yearly average of each student in the class, sending a message only to those approved, until the last student arrives there, the program closes.

What the next exercise asks you to transform this code and make it calculate the overall average of the whole class after all the individual averages are entered the only way I found to do this was to store the sum of ALL the% % in ONE variable only and dividing this variable by media_semestral . but I could not find a way to efficiently create this variable.

    
asked by anonymous 31.08.2017 / 17:50

1 answer

1

The path is this one, there is no magic.

qntd_alunos = int(input("Digite a quantidade de alunos: "))
qnt = 0
media_geral = 0
while qnt <= qntd_alunos-1:
    MB1 = float(input("Digite a média do primeiro bimestre: "))
    MB2 = float(input("Digite a média do segundo bimestre: "))
    media_semestral = (MB1 + MB2) / 2
    media_geral += media_semestral
    if media_semestral >= 7:
        print("Você foi aprovado")
        print("Sua média semestral é: ", media_semestral)
    else:
        print("Sua média semestral é: ", media_semestral)
    qnt = qnt+1
    if qnt == 1:
        print(qnt, "aluno já recebeu sua média, faltam", qntd_alunos - qnt)

    else:
        print(qnt, "alunos já receberam suas médias, faltam", qntd_alunos - qnt)
print("Média geral ", media_geral / qtd_alunos)

See running on ideone . And no Coding Ground . Also I put GitHub for future reference .

    
31.08.2017 / 17:59