Python Programming Tutorial 10, Conditionals.


Conditionals(if, elif, else)

In this lesson, we will continue talking about conditionals. We already wrote a simple conditional structure in the previous lesson; now we are going to make it more complex. We learned that the 'if ' structure is a logical structure. It evaluates boolean values to decide whether to execute blocks of code or not.

We are going to create a program to evaluate the grades of a student and see how we can test multiple conditions using 'nested if structures'.

Firstly, we would ask for grades to test made by a student, so grade 1 and grade 2. Then ask for the number of absences, which is an integer value, and the total number of classes. Now that we have the inputs, we can calculate the average grades. using Python code as seen below.

These are our parameters we are going to evaluate with conditionals:





 

 

Let's calculate:









Rules:

- Grades are 0 to 10

- Students need an average grade of 6 or higher to get approval

- Students need an attendance rate of 80% or higher to get approval

There will always be multiple solutions for the same problem. Let's start with one solution that is going to use ''nested 'if' blocks'' to solve the problem. Let's start by using our conditionals.

Let's start by checking if the average grade is greater than or equal to 6. If this is true, we don't know if the student was approved yet. We still need to check if he had more than 80 percent of the attendance rate, so inside the 'if'' structure, we are going to open another 'if structure'. If the code runs from here, if (avg_grade >= 6), to here, if (attendance >= 0.8), it means that the average grade is higher than 6 and the attendance rate is higher than 0.8 or 80 percent, and that means the student was approved. Otherwise, we must run an 'else statement' that he was not approved or has failed.

After all, we still need another 'else statement' to be a part of the 'if' structure. In this case, the student has also failed. We can still do some improvements, but first let's test if this is working!

See below:
















Let's run the code to see if it's working:











Now let's try grade 7 and 2 absences in 20 classes:










In this case the student has been approved, which is correct. 

Let's try it again: 









This time the student has failed because the average grade 5 was less than 6.

Let's try one last time:








The student has failed because the attendance rate was lower than 80 percent. 

We just solve our problem using nested if structures that are if structures inside if structures. It would be a bit better if we knew the reason why the student has failed, right? So let's complete this program. In case we are in this yellow highlighted else statement, 


 








It means that the average grade is higher than 6, but the attendance rate is lower than 80 percent, or 0.8. However, if we get to the last else statement or the code runs to the last statement, it means the average grade was less than 6 in due to the grade, but we don't know about the attendance, so let's write another line of code. In this case, we will start the elif statement. If we get to this elif statement, it means this: if (avg_grade >= 6) has failed. So we will test attendance again, and if the attendance is greater than 0.8, then we know the student failed due to a grade lower than 6. If we get to the else statement, it means the grade is not lower than 6 and the attendance rate is not greater than 0.8, and this means the student failed due to lower grade and lower attendance.

So let's complete the statement, having that in mind:

 









Now before we test this, let's just print the average grade and the attendance rate before giving the result. We can round the average grade to 2 decimal places and multiply the attendance rate by 100 because it is in decimal form. So we will multiply by 100, round it, and convert it to a string so we can concatenate (+) with a percent sign (%). 

Now let's see if this is working:










And our code is beautifully working. Let's try again:









And it seems to be working fine. We should always test all our conditions.








It's working, so let's check the last case which is for both reasons:







Now we have a working example. There are still some problems with our program but I will talk about them and teach you how to fix them later in this section.

In the next lesson we will learn how to use use less lines of code for the same program by using the 'AND' or 'OR' operator.

I'll see you then.









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...