How to interrupt the algorithm?

Asked

Viewed 54 times

-2

I am not able to interrupt the execution of the program, in case the student is approved without recovery. I have tried Return and break, but, it did not work.

n1 = int(input('Insira a nota da prova 1: '))
n2 = int(input('Insira a nota da prova 2: '))

media = (n1 + n2) / 2


print ('A média é: ', media)

if media > 5:
    print ('Aprovado')
else:
    print ('Reprovado')
         
if media < 5:
    recuperação = int(input('Nota da recuperação: '))
    media_rec = (media + recuperação)/2
    print ('A média é: ', media_rec)
if media_rec > 5:
    print ('Aprovado')
else:
    print ('Reprovado')`

2 answers

3

Expensive,

Identation in Python is crucial.

His last if forehead media_rec will be executed regardless of whether the student has passed or not and this variable is only initialized if the student is in recovery.

Two solutions

1 - Make media_rec equal to media. This is an ugly solution and will get a bug, because if Aprovado the message will appear twice

n1 = int(input('Insira a nota da prova 1: '))
n2 = int(input('Insira a nota da prova 2: '))

media = (n1 + n2) / 2

media_rec = media

print ('A média é: ', media)

if media > 5:
    print ('Aprovado')
else:
    print ('Reprovado')
         
if media < 5:
    recuperação = int(input('Nota da recuperação: '))
    media_rec = (media + recuperação)/2
    print ('A média é: ', media_rec)
if media_rec > 5:
    print ('Aprovado')
else:
    print ('Reprovado')

2 - Correct ident and improve the if

n1 = int(input('Insira a nota da prova 1: '))
n2 = int(input('Insira a nota da prova 2: '))

media = (n1 + n2) / 2


print ('A média é: ', media)

if media >= 5:
    print ('Aprovado')
elif media < 5:
    print ('Reprovado')
    recuperação = int(input('Nota da recuperação: '))
    media_rec = (media + recuperação)/2
    print ('A média é: ', media_rec)
    if media_rec > 5:
        print ('Aprovado')
    else:
        print ('Reprovado')

I hope it helps.

2


I couldn’t understand your doubt, because I ran your code here and it all worked out! If the problem is that when the student is approved, the other if block also passes the test by returning "approved" twice is quite simple to solve, just move the recovery note block inside your first Else block getting that way:

`n1 = int(input('Insira a nota da prova 1: '))
n2 = int(input('Insira a nota da prova 2: '))

media = (n1 + n2) / 2

media_rec = media

print ('A média é: ', media)

if media > 5:
    print ('Aprovado')
else:
    if media < 5:
        recuperação = int(input('Nota da recuperação: '))
        media_rec = (media + recuperação)/2
        print ('A média é: ', media_rec)
    if media_rec > 5:
        print ('Aprovado')
    else:
        print ('Reprovado')`

As you can see I have only identented 2° if/Else block within 1° I and removed one of the two instructions "print('failed')

As for the reason of neither the return, nor the break nor the return break function is that they are "methods" (so to speak) specific to other logical structures and functions. The "Return" only works and is valid in a function in your case if you had taken this code and turned it into a function and asked the function to return a print of the student’s state. "Break" is unique to the "while" loop, which in your case is used to approve/fail n students, that is, while there are n students repeat the assignments questions and etc. When there are no more n students (which can be a user input) break that would stop the repetition.

  • 1

    Complementing: the break can be used within a for also.

  • Correct! I forgot to talk about this case, but thanks for the supplement

Browser other questions tagged

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