I’m having repeat problems in python

Asked

Viewed 142 times

0

while True:
  nome = str(input('nome do aluno: '))
if nome == 'sair':
   break
elif nome in notas:
  print(f'\n{nome}',nome.upper())

n1 = float(input('Nota do aluno: '))
n2 = float(input('Segunda nota do aluno: '))
n3 = float(input('terceira nota do aluno: '))
n4 = float(input('Quarta nota do aluno: '))
m = (n1 + n2 + n3 + n4) / 4

print('media do {}: {:.1}'.format(nome,m))

if m < 5.0:
      print('reprovado')
elif m < 10:
    print('aprovado')
else:
    print('aprovado com Distinção!')
  • Put an explanation of your problem, how you want your code to work for the person who will help you, don’t disfigure your program. For example I could say that removing the line while True: your program will run but you put there for a reason, want something to repeat indefinitely, and other problems will arise with this code.

  • In Elif name in notes: what comes to be notes?

  • Whereas indentation errors were only misspellings.

2 answers

1

Note that in python your identation have an impact on the algorithm, so it is easy to notice your error... note that:

while True:
  nome = str(input('nome do aluno: '))

is a loop whose scope has only:

nome = str(input('nome do aluno: '))

so this loop does not have a condition for it to be finalized. To solve this problem just fix the identation of your program, staying the way you:

while True:
    nome = str(input('nome do aluno: '))
    if nome == 'sair':
        break
    elif nome in notas:
        print(f'\n{nome}',nome.upper())

    n1 = float(input('Nota do aluno: '))
    n2 = float(input('Segunda nota do aluno: '))
    n3 = float(input('terceira nota do aluno: '))
    n4 = float(input('Quarta nota do aluno: '))
    m = (n1 + n2 + n3 + n4) / 4

    print('media do {}: {:.1}'.format(nome,m))

    if m < 5.0:
        print('reprovado')
    elif m < 10:
        print('aprovado')
    else:
        print('aprovado com Distinção!')

However, notice that on this line:

elif nome in notas:

you have not set 'notes' previously, so the IDE returns:

Nameerror: name 'notes' is not defined

To solve this problem just remove that part of the code, remaining so:

elif nome:

in addition, the way You formatted the string, the name will be repeated 2 times, one of the way the user typed and the other in uppercase:

print(f'\n{nome}',nome.upper())

Exit:

Jorgim JORGIM

and to solve just put the nome.upper() within the {} and your final code will look like this:

while True:
    nome = str(input('nome do aluno: '))
    if nome == 'sair':
        break
    elif nome:
        print(f'\n{nome.upper()}')

    n1 = float(input('Nota do aluno: '))
    n2 = float(input('Segunda nota do aluno: '))
    n3 = float(input('terceira nota do aluno: '))
    n4 = float(input('Quarta nota do aluno: '))
    m = (n1 + n2 + n3 + n4) / 4

    print('media do {}: {:.1}'.format(nome,m))

    if m < 5.0:
        print('reprovado')
    elif m < 10:
        print('aprovado')
    else:
        print('aprovado com Distinção!')

0

Good morning David

Dude, looking at your code, there will be no loop to any other item except for the part:

  nome = str(input('nome do aluno: '))

Note that in Python identation is fundamental and, if you want all operations to stay inside the loop, you should ident your code as follows:

while True:
    nome = str(input('nome do aluno: '))
    if nome == 'sair':
       break
    elif nome in notas:
      print(f'\n{nome}',nome.upper())

    n1 = float(input('Nota do aluno: '))
    n2 = float(input('Segunda nota do aluno: '))
    n3 = float(input('terceira nota do aluno: '))
    n4 = float(input('Quarta nota do aluno: '))
    m = (n1 + n2 + n3 + n4) / 4

    print('media do {}: {:.1}'.format(nome,m))

    if m < 5.0:
          print('reprovado')
    elif m < 10:
        print('aprovado')
    else:
        print('aprovado com Distinção!')

Browser other questions tagged

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