Problems with conditional structures

Asked

Viewed 837 times

4

I started programming a little while ago and I’m doing some Python exercises.

The problem is that the result variable always returns "Approved" even when the concept is "D" or "E".

I’ve broken my head a lot and I can’t see the error.

Do a program that reads the two partial grades earned by a student in a discipline over a semester, and calculate their average. The assignment of concepts follows the table below: Mean of Concept Harnessing Between 9.0 and 10.0 A Between 7.5 and 9.0 B Between 6.0 and 7.5 C Between 4.0 and 6.0 D Between 4.0 and zero E The algorithm shall display the notes, the mean, the corresponding concept and the "OK" message on the screen if the concept is A, B or C or "FAILED" if the concept is D or E.

nota1=float(input("Digite nota 1: "))
nota2=float(input("Digite nota 2: "))
media=(nota1+nota2)/2
if media >=9:
   conceito = "A"
elif media >= 7.5:
   conceito = "B"
elif media >= 6:
    conceito = "C"
elif media >= 4:
    conceito = "D"
elif media >= 0:
    conceito = "E"
if conceito == "A" or "B" or "C":
    resultado = "Aprovado!"
elif conceito == "D" or "E":
    resultado = "Reprovado"
print("Nota 1: %.2f\nNota 2:%.2f"%(nota1,nota2))
print("Média: %.2f"%media)
print("Conceito: %s"%conceito)
print("Resultado: %s"%resultado)

4 answers

4


Just change that:

if conceito == "A" or "B" or "C":
    resultado = "Aprovado!"
elif conceito == "D" or "E":
    resultado = "Reprovado"

for that reason:

if conceito == "A" or conceito == "B" or conceito == "C":
    resultado = "Aprovado!"
elif conceito == "D" or conceito == "E":
    resultado = "Reprovado"

or if you prefer:

if conceito in ("A","B","C"):
    resultado = "Aprovado!"
elif conceito in ("D", "E"):
    resultado = "Reprovado"

and ready.

  • Antony, please also explain the reason for the change. Not all users will have the abstraction level enough to understand the difference between the codes.

4

Disclaimer: It’s obvious that the code can be a little more idiomatic and Pythonic without needing to change a lot, but for me, it doesn’t make sense to try to explain it now because as the AP said (and also by the very nature of the question) it’s starting and trying to understand basic things.

For all intents and purposes, the code could be like this

resultado = "Aprovado!" if conceito in ("A", "B", "C") else "Reprovado"

The problem is that the condition is not right, it should be like this.

if conceito == "A" or conceito == "B" or conceito == "C":
    resultado = "Aprovado!"
elif conceito == "D" or conceito == "E":
    resultado = "Reprovado"

The way you wrote it, you’ll always fall for the first if because you don’t compare the value of conceito with "B" or with "C".

You simply throw their values into the condition and B (as well as C) are interpreted as true by Python. That’s because whichever nonempty string is true for Python.

You can check this behavior in repl.it.

That is, the condition you wrote could be read like this

If the value of conceito is equal to "A" (que é false) or if "B" (que é true) or if "C" (que também é true), do (...)

And she should be like this

If the value of conceito is equal to "A" (que é false) or if the value of conceito is equal to "B" (que é false) or if the concept value is equal to "C" (que é false) do (...)

See working on repl.it.

1

You are making incorrect use of the conditions. Do so :

nota1=float(input("Digite nota 1: "))
nota2=float(input("Digite nota 2: "))
media=(nota1+nota2)/2
if media >=9:
   conceito = "A"
elif media >= 7.5:
   conceito = "B"
elif media >= 6:
    conceito = "C"
elif media >= 4:
    conceito = "D"
else:               # Não é necessário utilizar o elif já que só resta uma opção 
    conceito = "E"
if conceito == "A" or "B" or "C":
    resultado = "Aprovado!"
else:                       #Mesma coisa aqui se conceito não é A,B,C é D ou E
    resultado = "Reprovado"
print("Nota 1: %.2f\nNota 2:%.2f"%(nota1,nota2))
print("Média: %.2f"%media)
print("Conceito: %s"%conceito)
print("Resultado: %s"%resultado)

I hope I’ve helped !

  • 1

    Please post the code in text, not images.

0

Instead of trying to define "approval" by the letters after the "concept" process you could work again with the media.

So still getting independent of each other.

nota1=float(input("Digite nota 1: "))
nota2=float(input("Digite nota 2: "))
media=(nota1+nota2)/2
if media >=9:
   conceito = "A"
elif media >= 7.5:
   conceito = "B"
elif media >= 6:
    conceito = "C"
elif media >= 4:
    conceito = "D"
elif media >= 0:
    conceito = "E"

resultado = "Aprovado!"
if media <= 4:
    resultado = "Reprovado"

print("Nota 1: %.2f\nNota 2:%.2f"%(nota1,nota2))
print("Média: %.2f"%media)
print("Conceito: %s"%conceito)
print("Resultado: %s"%resultado)

Browser other questions tagged

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