Python Programming Tutorial 14.

 Data Validation

In this lesson we are going to talk about data validation, and we are going to use the program we created for the grades of the student. 

..................................................................................................................................................................
I'm referring to this data:
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%")

..................................................................................................................................................................

Let's talk about some problems that might occur in this program. 
In this data, there's no data validation, so even grades from 0 to 10, we can type a 100 by accident, and the program is going to accept it. 
Another program is that if I type a string, our program crashes, which is really tragic. Before we see how we can do basic data validation, let's visualize this: 










And we had an error after running this code and answering with the above information. In our next lesson, we will keep our program crashing by doing error handling.

Data validation

So before asking for user input, we are going to create a variable called "data_valid" and assign to it a value of False. { data_valid  =  False }.
So let's start a while loop with this information: 











Now, the highlighted text is going to return true because data_valid is equal to false, so we are going to go inside this while loop. Afterwards, we will request the grade and perform the validation using an if statement.
 
data_valid = False

while data_valid == False:
        grade1 = float ( input("Type the grade of the first test: ") )
        if grade1 < 0 or grade1 > 10:
                print("Grade should be between 0 and 10")
                continue
        else:
                data_valid = True

This is what it will look like. So we just went inside the while loop and asked for the grade, and we made the validation with an if condition. You do notice that we use the'or'operator, so if one of the tests results true, the whole test is going to result true. If this happens, it means we have an invalid input, and in this case we are going to print the range of the grade. Then we are going to continue in this loop; using the continue function, the continue will jump out of this loop and start over again. Then, we complete with an else statement because if this if grade1 < 0 or grade1 > 10 is not true, it means we have valid input, so in this case, we are going to make data_valid = true. This is going to end the loop, and when it tries to run again, this test data_valid == False is going to return in false, so it is not going to go inside the loop and it's going to continue with our program. Now we are going to do the exact same thing for grade 2.

Let's copy all the data, and set the value of data_valid to false again.

data_valid = False


while data_valid == False:

        grade1 = float ( input("Type the grade of the first test: ") )

        if grade1 < 0 or grade1 > 10:

                print("Grade should be between 0 and 10")

                continue

        else:

                data_valid = True


data_valid = False


while data_valid == False:

        grade2 = float ( input("Type the grade of the second test: ") )

        if grade2 < 0 or grade2 > 10:

                print("Grade should be between 0 and 10")

                continue

        else:

                data_valid = True

This is how our code will look like. Before we continue, let's test it to see what happens.






I first tried negative ten, and it didn't work, it said grade should be between zero and 10. I tried eleven, and it still didn't work. Now, our program only accepts numbers between zero and ten, so let's try eight for the first test and minus ten for the second test. 





It still didn't work. 

Alright, let's continue. For the absences in total classes, lets do something similar, but let's change the order and ask for the total classes first. Let's copy the data one more time. 

 data_valid = False


while data_valid == False:

        total_classes = int ( input("Type the the total number of classes: ") )

        if total_classes <= 0:

                print("The number of classes can't be zero or less")

                continue

        else:

                data_valid = True

We asked for the total number of classes, and we tested if total classes were less than 0. We can have a negative number of classes here, but we won't put any limitation on the number of classes. Actually, this is going to be a bit different since we can have zero classes, and hence the less than or equal to sign was used. So we printed or told the user that the number of classes can't be zero or less.
 
Now let's do the same for the absences. 
We won't accept absences less than zero. We can't accept zero and we can't accept the number of absences greater than the total number of classes. 

data_valid = False

while data_valid == False:
        absences = int ( input("Type the number of absences: ") )
        if absences <= 0 or absences > total_classes:
                print("The number of absences can't be less than or greater than the number of total classes")
                continue
        else:
                data_valid = True
Alright, let's run this and see. 











The validation is working fine. Well this is better but our program crashes when we type in a string, so in our next lesson, we are going to do some error handling. 

Lines of code used: 

data_valid = False

while data_valid == False:
        grade1 = float ( input("Type the grade of the first test: ") )
        if grade1 < 0 or grade1 > 10:
                print("Grade should be between 0 and 10")
                continue
        else:
                data_valid = True

data_valid = False

while data_valid == False:
        grade2 = float ( input("Type the grade of the second test: ") )
        if grade2 < 0 or grade2 > 10:
                print("Grade should be between 0 and 10")
                continue
        else:
                data_valid = True
                
data_valid = False

while data_valid == False:
        total_classes = int ( input("Type the the total number of classes: ") )
        if total_classes <= 0:
                print("The number of classes can't be zero or less")
                continue
        else:
                data_valid = True

data_valid = False

while data_valid == False:
        absences = int ( input("Type the number of absences: ") )
        if absences <= 0 or absences > total_classes:
                print("The number of absences can't be less than or greater than the number of total classes")
                continue
        else:
                data_valid = True


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%")
     
        
Goodluck 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...