Code does not work [PYTHON]

Asked

Viewed 73 times

-2

I wrote a simple script that captures student grades through an auxiliary list and then plays everything within a larger list, including the average of students that is calculated during the code. I created a while with the 999 flag to exit the iteration and the program allows the user (who could be a teacher for example) to access the student’s notes to understand the average that is shown in the menu. The user needs to enter an indexer number if he or she enters a out of range the program gets stuck in the loop until it enters a valid number. The problem is that after I once type a non-existent indexer it remains stuck in the loop even after I enter a valid indexer. Follows code:

dados = []
geral = list()
while True:
    dados.append(input('Nome do aluno: '))
    dados.append(float(input('Nota 1: ')))
    dados.append(float(input('Nota 2: ')))
    media = (dados[1] + dados[2]) / 2
    dados.append(media)
    geral.append(dados.copy())
    dados.clear()
    resp = ' '
    while resp not in 'SN':
        resp = input('Quer cadastrar mais alunos? [S/N] ').strip().upper()[0]
    if resp == 'N':
        break
print('-' * 35)
print('ALUNOS CADASTRADOS'.center(35))
print('-' * 35)
print(f'{"Código":<8}{"Nome":<17}{"Média Final":5}')
for i, (nome, n1, n2, media) in enumerate(geral):
    print(f'{i:<8}{nome:<15}{media:5}')
print()
relatorio = 0
while relatorio != 999:
    relatorio = int(input('Você quer analisar as notas de qual aluno? (DIGITE 999 para PARAR) '))
    while relatorio not in range(len(geral)):
        relatorio = input(f'Aluno não existente. Digite um número entre 0 e {len(geral) - 1}: ')
    print(f'Histórico de notas do aluno {geral[relatorio][0]}: ', end='')
    print(f'{geral[relatorio][1]}, {geral[relatorio][2]}')
print('PROGRAMA DE NOTAS ENCERRADO.')

1 answer

2


A solution is to validate if the value you enter exists the number of positions in Dict, "general variable".

file py.

dados = []
geral = list()
while True:
    dados.append(input('Nome do aluno: '))
    dados.append(float(input('Nota 1: ')))
    dados.append(float(input('Nota 2: ')))
    media = (dados[1] + dados[2]) / 2
    dados.append(media)
    geral.append(dados.copy())
    dados.clear()
    resp = ' '
    while resp not in 'SN':
        resp = input('Quer cadastrar mais alunos? [S/N] ').strip().upper()[0]
    if resp == 'N':
        break
print('-' * 35)
print('ALUNOS CADASTRADOS'.center(35))
print('-' * 35)
print(f'{"Código":<8}{"Nome":<17}{"Média Final":5}')
for i, (nome, n1, n2, media) in enumerate(geral):
    print(f'{i:<8}{nome:<15}{media:5}')
print()
relatorio = 0
while relatorio != 999:
    relatorio = int(input('Você quer analisar as notas de qual aluno? (DIGITE 999 para PARAR) '))
    if relatorio < len(geral):
      print(f'Histórico de notas do aluno {geral[relatorio][0]}: ', end='')
      print(f'{geral[relatorio][1]}, {geral[relatorio][2]}')
print('PROGRAMA DE NOTAS ENCERRADO.')

Browser other questions tagged

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