Manipulating String while true PYTHON

Asked

Viewed 193 times

0

I’m doing a student grade exercise, receiving a str with the student’s name, then int to know the grades(n1,N2,N3,N4). I made a while true, so it will always ask, until the user asks to quit, because the program drops asking if you want to continue with s/n.

while True:
aluno_nome = str(input("Digite o nome do aluno:"))
aluno = print("Digite notas aluno.")
n1=int(input("Nota 1:"))
n2=int(input("Nota 2:"))
n3=int(input("Nota 3:"))
n4=int(input("Nota 4:"))
final = (n1+n2+n3+n4)/4
#print("Aluno ",aluno_nome,"\nNota Final:",media)
sai = str(input('\nDeseja continuar[S/N]?').lower())
while sai != 's' and sai != 'n':
    sai = str(input('Deseja continuar[S/N]?').lower())
if sai in 'n':
    break
print("\nAluno ",aluno_nome,"\nNota Final:",final)

I want to know the easiest way, to print at the end the name of each student, and the grade of the same, such student in such, such student grade. I have to actually create a variable for each name and so save each of the 4 notes inside, but I can’t do it more "clean" with while true, and and do some counter to go creating and storing the name and final grade of the student.

  • 1

    Do you want to show at the end of the loop a list of all input students and their final average? Would that be it?

1 answer

0

You can use dictionary to save the data of each student, use a list to save them and when the user finishes typing, you make an iteration in the student list to display the final result, e.g.:

# cria um array de alunos
ar_alunos = []

while True:
  al = {} # cria dicionario aluno vazio

  # salva nome do aluno
  al['aluno_nome'] = str(input("Digite o nome do aluno:"))

  print("Digite notas aluno.")
  n1=int(input("Nota 1:"))
  n2=int(input("Nota 2:"))
  n3=int(input("Nota 3:"))
  n4=int(input("Nota 4:"))

  #calcula media das notas
  al['media'] = (n1+n2+n3+n4)/4

  #salva aluno no array de alunos
  ar_alunos.append(al)

  sai = str(input('\nDeseja continuar[S/N]?').lower())
  while sai != 's' and sai != 'n':
      sai = str(input('Deseja continuar[S/N]?').lower())
  if sai in 'n':
      break


# printa todas as medias  
print()
print("# Relatorio de medias #")
for aluno in ar_alunos:
  print()
  print("Aluno ",aluno['aluno_nome'],"\nNota Final:",aluno['media'])

Browser other questions tagged

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