Problems with conditional structures

4

I started programming shortly 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 have already broken my head and can not see the error.

  

Make a program that reads the two partial marks obtained by a student in a course over a semester, and calculate their average. The assignment of concepts obeys the table below:   Average Utilization Concept   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 should display the notes, the mean, the corresponding concept and the message "APPROVED" 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)
    
asked by anonymous 18.08.2017 / 22:18

4 answers

4

Just change this:

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

for this:

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 that's it.

    
18.08.2017 / 22:23
4
  

Disclaimer : Obviously, the code may be a little more idiomatic and Pythonic without changing a lot, but for me it does not make sense to try to explain this now because as the AP said also by the very nature of the question) he is starting and trying to understand basic things.

     

For every effect, the code could be like that

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, it will always fall into the first if because you do not compare the value of conceito with "B" or "C" .

You simply throw their values into the condition and B (as well as C ) are interpreted as true by Python. This is because any non-empty string is true for Python.

You can check this behavior on 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" (% with%), do (...)

And it should be so

  

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

See working on repl.it.

    
18.08.2017 / 22:23
1

You are making incorrect use of the conditions. Here's how:

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 have helped!

    
18.08.2017 / 23:59
0

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

So still getting one independent of the 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)
    
18.08.2017 / 22:55