Problem to find the lowest value

Asked

Viewed 58 times

-1

Good evening, I’m having trouble displaying the lowest value in my code. The statement is:

Develop a program to check the student’s grade in a test with 10 questions, the program should ask the student the answer of each question and at the end compare with the test template and thus calculate the total hits and the grade (assign 1 point per right answer). After each student uses the system a question should be asked if another student will use the system. After all students have responded inform:

Major and Minor Hit; Total of Students who used the system; The Grade Average Class

My code:

from termcolor import  colored
tot = total_alunos = maior = valor = menor = 0
while True:
    tot = 0
    for c in range(1, 11):
        resp = input('Resposta da {}° questão: '.format(c)).strip().upper()
        if c == 1:
            gabarito = 'A'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 2:
            gabarito = 'B'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 3:
            gabarito = 'C'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 4:
            gabarito = 'D'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 5:
            gabarito = 'E'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 6:
            gabarito = 'E'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 7:
            gabarito = 'D'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 8:
            gabarito = 'C'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 9:
            gabarito = 'B'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 10:
            gabarito = 'A'
            if resp == gabarito:
                tot += 1
                valor += 1
        if tot > maior:
            maior = tot
        if tot < menor:
            menor = tot
    print(f'Você acertou no total {tot} questoes e sua nota foi {tot} pontos')
    total_alunos = total_alunos + 1
    media = valor / total_alunos
    continuar = ''
    while continuar != 'S' and continuar != 'N':
        continuar = input('Você deseja continuar? [S/N]: ').upper().strip()[0]
        if continuar not in 'SN':
            print(colored('ERRO! Digite novamente', 'red'), end=' ')
    if continuar == 'N':
        break
print(f'O maior foi {maior} o menor foi {menor}')
print(f'O total de alunos que utilizaram o sistema foi {total_alunos}')
print(f'A média da turma foi {media}')
  • First of all, Merry Christmas rs. Your question is a little confusing, try to be clearer and explain the situation better, put the result you are getting and so it is easier to help, so I noticed you are learning still, but if you already know, it would be advisable to use list to store the results and even to reduce the size of your for

1 answer

0

I think I understand your problem

he is present in that part

if tot < menor:
    menor = tot

he checks whether the total is less than menor whereas the menor is equal to 0, then in fact he will never be given as True

what you can do?

if menor == maior == 0:
    menor = maior = tot
elif tot > maior:
    maior = tot
elif tot < menor:
    menor = tot

and remove that part of your code from within your for

to make it easier to understand, I’ll leave the complete code below

from termcolor import colored
tot = total_alunos = maior = valor = menor = 0
while True:
    tot = 0
    for c in range(1, 11):
        resp = input('Resposta da {}° questão: '.format(c)).strip().upper()
        if c == 1:
            gabarito = 'A'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 2:
            gabarito = 'B'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 3:
            gabarito = 'C'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 4:
            gabarito = 'D'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 5:
            gabarito = 'E'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 6:
            gabarito = 'E'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 7:
            gabarito = 'D'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 8:
            gabarito = 'C'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 9:
            gabarito = 'B'
            if resp == gabarito:
                tot += 1
                valor += 1
        elif c == 10:
            gabarito = 'A'
            if resp == gabarito:
                tot += 1
                valor += 1
    if menor == maior == 0:
        menor = maior = tot
    elif tot > maior:
        maior = tot
    elif tot < menor:
        menor = tot
    print(f'Você acertou no total {tot} questoes e sua nota foi {tot} pontos')
    total_alunos += 1
    media = valor / total_alunos
    continuar = ''
    while continuar != 'S' and continuar != 'N':
        continuar = input('Você deseja continuar? [S/N]: ').upper().strip()[0]
        if continuar not in 'SN':
            print(colored('ERRO! Digite novamente', 'red'), end=' ')
    if continuar == 'N':
        break
print(f'O maior foi {maior} o menor foi {menor}')
print(f'O total de alunos que utilizaram o sistema foi {total_alunos}')
print(f'A média da turma foi {media}')

Browser other questions tagged

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