Python Programming tutorial 11.

 AND / OR operators

In our previous lesson, we created a program to calculate the grades and output the results of a student. We used the nested if structure to solve our problem. And now we are going to learn two new operators, the AND or OR operators, and solve this problem in a different way. What those operators do is test multiple conditions at the same time. So watch the alternate solution to the nested if structures; however, with the 'and' operator, the two conditions/test must be true in order to know if the student was approved. Let's visualize this:

NB: refer to our previous tutorial on conditionals.

Instead of this nested if structure:





we can test these two conditions at the same time by using the 'and' operator and check if the attendance rate is greater than 80 percent.




The print statement is going to run if both of the conditions before and after the and operator are true.

After that, we can start an 'elif' statement and test the opposite condition:





In this case we know the student has failed for both reasons. Let's do another elif statement:






If we enter here, we know that the student has failed due to an average grade lower than 6. If he had failed for both reasons, the code would have ended at the first elif statement. As a result, he hasn't been approved and he hasn't failed for both reasons. Also the average grade is greater than 0.8, so he failed because of his grade. 

Now we need an else statement:







We know if he didn't fail because of his grade then he probably failed because of his lower attendance rate. This is a different way of solving the same problem so let's test if this is working.








And he has been approved. Let's try some more:







Now he has failed for both reasons.













And this is how the 'and' operator works. The ' or ' operator works similar. The difference is that when we use 'or' only one condition has to be true for the test to return true:







So if the highted returns true and the attendance rate returns false, this is okay. Only one has to be true for it to run. That is how we use the 'OR' statement or operator. This is not going to be useful to use the or operator, but we are going to ge the opportunity to use it more later in the course. 

This was all for today's lesson, I will see you in the next tutorial.  


Summary of code:

grade1 = float ( input("Type the grade of the first test: ") )    
grade2 = float ( input("Type the grade of the second test: ") )
absences = int ( input("Type the number of absences: ") )
total_classes = int ( input("Type the the total number of classes: ") )

avg_grade = (grade1 + grade2) / 2
attendance = (total_classes - absences) / total_classes

print("Average grade: ", round(avg_grade,2) )
print("Attendance rate: ", str(round((attendance * 100),2))+'%' )

if (avg_grade >= 6 or attendance >= 0.8):
        print("The student has been approved.")
elif(avg_grade < 6 and attendance < 0.8):
    print("The student has failed due to an average grade less than 6.0 and an attendance rate less than 80%.")
elif(attendance >= 0.8):
     print("The student has failed due to an average grade less than 6.0.")
else:
    print("The student has failed due to an attendance rate less than 80%")
     
        

These are the lines of code use for the lesson, see you next time.



No comments:

Post a Comment

Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...