format a python list

Asked

Viewed 78 times

1

I can’t format the line print('{0:>20}'.format('Alunos:'),melhores) where I have to show the best students how it looks in the picture, can help me?exemplo de resposta

# coding: iso-8859-1 -*-
import math
nAlunos=0
while True:
    while True:
        nAlunos=eval(input('Indique o número de alunos:'))
        if 1<=nAlunos<=100:
            break
        else:
            print('O número de alunos tem de ser entre 1 e 100');print()
    ListaNomes=[0 for i in range(0,nAlunos)]
    ListaNotas=[0 for i in range(0,nAlunos)]
    reprovados = suficiente = bom = muitobom = 0
    for i in range(0,nAlunos):
        ListaNomes[i]=input('Nome:')
        while True:
            ListaNotas[i]=eval(input('Nota:'))
            if 0<=ListaNotas[i]<=20:
                if 0<=ListaNotas[i]<10:
                    reprovados=reprovados+1
                elif 10<=ListaNotas[i]<14:
                    suficiente=suficiente+1
                elif 14<=ListaNotas[i]<17:
                    bom=bom+1
                elif 17<=ListaNotas[i]<20:
                    muitobom=muitobom+1
                break
            else:
                print('A nota tem de estar entre 0 e 20');print() 
    PercentagemReprovados=(reprovados*100)/len(ListaNotas)
    PercentagemSuficiente=(suficiente*100)/len(ListaNotas)
    PercentagemBom=(bom*100)/len(ListaNotas)
    PercentagemMuitobom=(muitobom*100)/len(ListaNotas)
    print();print('{0:>20}{1:>15}'.format('NOME','NOTA'));print()
    for i in range(0,nAlunos):
        print('{0:>20}{1:>15.1f}'.format(ListaNomes[i],ListaNotas[i]))
    print();print()
    print('{0:>20}{1:>15}{2:1}'.format('Reprovados:',round(PercentagemReprovados),'%'))
    print();print('{0:>20}'.format('Aprovados:'))
    print('{0:>19}{1:>16}{2:1}'.format('suficiente',round(PercentagemSuficiente),'%'))
    print('{0:>19}{1:>16}{2:1}'.format('bom',round(PercentagemBom),'%'))
    print('{0:>19}{1:>16}{2:1}'.format('muito bom',round(PercentagemMuitobom),'%'));print()
    print('{0:>20}{1:>16.1f}'.format('Melhor nota:',max(ListaNotas)))
    melhor_nota=max(ListaNotas)
    melhores=[ListaNomes[i] for i in range(len(ListaNotas)) if ListaNotas[i] == melhor_nota]
    print('{0:>20}'.format('Alunos:'),melhores)
    print()
    resp=input('Repetir?(S/N)').upper()
    while (len(resp)!=1)or(resp not in 'SN'):
        print('Valor inválido!')
        resp = input('Repetir?(S/N)').upper()
    if resp=='N':
        break

1 answer

1


melhores is a list that can have multiple students, so how should it be formatted? One per line, all on the same line?

All in the same row I think is not a good option, because if you have several, will exceed the column size easily. So I think I’d better put one per line:

for aluno in melhores:
    print('{0:>20}{1:>16}'.format('Aluno:', aluno))

Example of leaving with more than one student:

        Melhor nota:            10.0
              Aluno:          fulano
              Aluno:        beltrano

If you are using Python >= 3.6 you can also use f-string in place of format:

for aluno in melhores:
    print(f'{"Aluno:":>20}{aluno:>16}')

Taking advantage, don’t use eval to convert what was typed to number, because besides being controversial, he hides some traps. Prefer to use int:

nAlunos = int(input('Indique o número de alunos:'))
  • I haven’t quite figured out how to use f-string instead of format, but thanks so much for the help and tip

Browser other questions tagged

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