Difficulties with using IF

Asked

Viewed 173 times

-2

Good morning, you guys. I’m still a beginner in programming, I’m trying to write a simple program that calculates the coefficient of performance and reports the percentage of the course completed, but I’m having problems with a conditional if that is not being respected. The idea is as follows, if the student’s situation is approved, I have an auxiliary variable that sums the credits obtained in the discipline, but it is only to add if it has been approved, however my code is ignoring the conditional and adding everything. Can someone help me figure out why this is happening?

for i in range(len(nota)):
if sit[i] == "aprovado\n" or "aprovado":
    aux += (cred[i]) #Calcula o somatório dos créditos nas matérias onde se obteve aprovação.

It is in the above section that the problem is. Below I will paste the whole code. It imports the notes from a txt file that has the amount of credits, the note and the situation. The txt file can be downloaded at https://drive.google.com/open?id=1TOjOD5ktD2eA0fEkkfFDg-HljuiKwHmN

Follows full code:

x = open('notas1.txt')
cred = []
nota = []
sit = []
cr = 0
sumcred = 0
totcred = 237 #Total de créditos do curso.
aux = 0
for line in x:
    a = line.split(" ")
    cred += [int(a[0])]
    nota += [float(a[1])]
    sit += [(a[2])]
for i in cred:
    sumcred += sum([i]) #Calcula o somatório dos créditos cursados até o momento.
for i in range(len(nota)):
    if sit[i] == "aprovado\n" or "aprovado":
        aux += (cred[i]) #Calcula o somatório dos créditos nas matérias onde se obteve aprovação.
print(aux)
for i in range(len(cred)):
    cr += cred[i]*nota[i] #Faz a multiplicação da nota obtida em cada disciplina pela quantidade de créditos.
print("O seu coeficiente de rendimento acumulado (CR) é igual a:",(round((cr/sumcred),2)))
print("O percentual concluído até o momento é de:",(round(((aux/totcred)*100),2)),"%")

1 answer

8


The construction of your condition is wrong:

if sit[i] == "aprovado\n" or "aprovado":
    ...

But before we begin, we need to know that a string not empty is considered as a true value in Python (Truthy value). That is, a value that if converted to boolean would be True.

In this way, the condition if "foo" will always be satisfied, as it would be the same as if True.

Analyzing your condition:

if sit[i] == "aprovado\n" or "aprovado":
    ...

Let’s assume that sit[1] hold the value "aprovado\n". First, the interpreter will evaluate the comparison sit[i] == "aprovado\n", who will return True, being like this:

if True or "aprovado":
    ...

This condition will always be satisfied, so when true "aprovado\n" he gets paroled.

Let’s assume now that sit[1] hold the value "aprovado". First, the interpreter will evaluate the comparison sit[i] == "aprovado\n", who will return False, being like this:

if False or "aprovado":
    ...

This condition will always be satisfied, because "aprovado" is true, therefore, when true "aprovado" he gets paroled.

Finally, let’s assume that sit[1] is worth any value other than the others already considered. First, the interpreter will evaluate the comparison sit[i] == "aprovado\n", who will return False, being like this:

if False or "aprovado":
    ...

This condition will always be satisfied, because "aprovado" is true, therefore, when true "aprovado" he gets paroled.

Concluding:

  • When sit[1] vale "aprovado\n" gets into the if;
  • When sit[1] vale "aprovado" gets into the if;
  • When sit[1] is worth any other value if;

That is, your condition does not depend on the value of sit[1] and could be replaced by:

if True:
    ...

However, how do you want to check whether sit[1] has one of the two elements, use the operator in:

if sit[1] in {"aprovado\n", "aprovado"}:
    ...

Or even better, as the difference is only the character \n at the end, just remove it before comparison:

sit[1] = sit[1].strip()

if sit[1] == "aprovado":
    ...

The function strip() will remove the \n of the end, can be compared only with a value.

How Python deals with logical operators has been discussed in this question:

  • It worked out! I still didn’t know the function strip(), I will write it down not to forget. Thank you very much.

Browser other questions tagged

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