Python Exercises - How to find the averages of 2 scores of a person | Python Exercise #4

Pybeginner
By -
0

This is a python program that calculates the average of 2 exam grades of a student. The program will work in the following order.


The first and foremost thing is to get the grades or scores of the student. To avoid choosing the variable type, we will be using the eval( ) method. This can help us to avoid confusion of choosing the variable type. So, it doesn’t matter whether it is an integer or float.


Now, it’s time to compute the averages of the scores given by the students and then we will print the result.


Tips to follow :

1. First get the student scores.

2. Compute the average.

3. Print the average.

 

Video Exlanation



Average of 2 scores:



# A simple program to calculate the average of two exam grades

# Illustrates the use of multiple input

 

print("This program calculates the average of two grades in exams.")

score_1, score_2 = eval(input("Enter two scores separated by a comma:"))

average = (score_1 + score_2) / 2

print("The average of the scores is:", average)


 

Average of 2 scores using functions :

Let us now transform the same code into a function and we can run the code by just calling the function.



def average():

    print("This program calculates the average of two grades in exams.")

    score_1, score_2 = eval(input("Enter two scores separated by a comma:"))

    average = (score_1 + score_2) / 2

    print("The average of the scores is:", average)

 

average()





Post a Comment

0Comments

Post a Comment (0)