Write a Python Program to Find The Average of The Best of Two Tests

Assuming you have three semester exams in a year,  and you have secured 60, 70 and 80  in the three semesters exam results.  Now your task is to take the best two semester results and find out the average of the best two results.

In the above example the best two results are 80 and 70.  Add 80 and 70 then dividing it by 2 will give us the average of the best two semester results. This is done manually, now let's try to solve it using a Python program.

 

 

Write a Python Program to Find The Average of The Best of Two Tests

 

 

Write a Python Program to Find The Average of The Best of Two Tests

I will be storing the marks obtained in test into a Python list, so let's take a length variable and ask users to enter the number of tests you gave in a year.

Take an empty list  of integers and ask the user to enter the number of marks obtained in all the semesters/ tests,  append the marks to the python list.

length = int(input("Enter Number of Tests You Gave : "))
results = []
for index in range(length):
  temp = int(input(f"Enter Marks Obtained in {index+1} Test : "))
  results.append(temp)



Now let's use the Bubble sort to sort the list into descending order.  Take two indexes i and j values and use the for loop to iterate up to list length and next for loop iterate up to length-i-1.

Now using the if statement, check if the neighboring list element results[j]  is less than results[j+1].  then swap the both the elements of list results[j], results[j+1]

for i in range(length):
   for j in range(length-i-1):
       if results[j] < results[j+1]:
           results[j], results[j+1] = results[j+1], results[j]




Once the for loop is completed you will be having the list containing the marks in descending order now using the print statement print the average.

Calculating the sum of the first and second index item of the list then dividing it by 2 will give the average of the best to marks obtained in the exam.

print("Average of The Best of Two Tests is : %.2f " % ((results[0] + results[1])/ 2))




Python Program 

=============================

length = int(input("Enter Number of Tests You Gave : "))
results = []
for index in range(length):
  temp = int(input(f"Enter Marks Obtained in {index+1} Test : "))
  results.append(temp)

for i in range(length):
   for j in range(length-i-1):
       if results[j] < results[j+1]:
           results[j], results[j+1] = results[j+1], results[j]

print("Average of The Best of Two Tests is : %.2f " % ((results[0] + results[1])/ 2))





Output
============
Enter Number of Tests You Gave : 5
Enter Marks Obtained in 1 Test : 4
Enter Marks Obtained in 2 Test : 3
Enter Marks Obtained in 3 Test : 2
Enter Marks Obtained in 4 Test : 4
Enter Marks Obtained in 5 Test : 1
Average of The Best of Two Tests is : 4.00




Output
==============
Enter Number of Tests You Gave : 3
Enter Marks Obtained in 1 Test : 1
Enter Marks Obtained in 2 Test : 2
Enter Marks Obtained in 3 Test : 3
Average of The Best of Two Tests is : 2.50




Output
===============
Enter Number of Tests You Gave : 3
Enter Marks Obtained in 1 Test : 30
Enter Marks Obtained in 2 Test : 40
Enter Marks Obtained in 3 Test : 40
Average of The Best of Two Tests is : 40.00




Conclusion
==================
Execute the above Python program by providing the number of tests you gave in a year and the marks obtained out of these tests.  Note down the results after executing the python.

This program will help you to Find The Average of The Best of Two Tests. Comment it down below if you have any suggestions to improve the above python program




Post a Comment

0 Comments