Difficulties with IF

0

Good morning, guys. I am still new to programming, I am trying to write a simple program, which calculates the coefficient of performance and report the percentage of the finished course, but I am having problems with an if conditional that is not being respected. The idea is this, if the student's situation is approved, I have an auxiliary variable that adds the credits obtained in the course, but only to add if it has been approved, however my code is ignoring the conditional and adding everything. Can anyone help me find 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 passage that there is the problem. Below I will paste the whole code. It imports the notes of a file type txt that has the amount of credits, the note and the situation. The txt file can be downloaded at link

Follow the complete 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)),"%")
    
asked by anonymous 28.12.2018 / 14:48

1 answer

4

The construction of your condition is wrong:

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

But before you start, you should know that a nonempty string is considered a true value in Python (truthy value). That is, a value that converted to Boolean would be True .

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

Analyzing your condition:

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

Suppose that sit[1] has the value "aprovado\n" . First, the interpreter will evaluate the sit[i] == "aprovado\n" comparison, which will return True , thus:

if True or "aprovado":
    ...

This condition will always be satisfied, so when it is valid "aprovado\n" it goes into the conditional.

Let's now assume that sit[1] has the value "aprovado" . First, the interpreter will evaluate the sit[i] == "aprovado\n" comparison, which will return False , thus:

if False or "aprovado":
    ...

This condition will always be satisfied, because "aprovado" is true, so when it is valid "aprovado" it goes into the conditional.

Finally, let's assume that sit[1] is worth any value different from the others already considered. First, the interpreter will evaluate the sit[i] == "aprovado\n" comparison, which will return False , thus:

if False or "aprovado":
    ...

This condition will always be satisfied, because "aprovado" is true, so when it is valid "aprovado" it goes into the conditional.

Concluding:

  • When sit[1] is "aprovado\n" enter if ;
  • When sit[1] is "aprovado" enter if ;
  • When sit[1] is worth any other value it enters if ;

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

if True:
    ...

However, since you want to check if sit[1] has one of the two elements, use the in :

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

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

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

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

The strip() function will remove the \n from the end, and can only compare with a value.

The way Python handles logical operators has been discussed in this question:

28.12.2018 / 15:10