In: Computer Science
Python: write a program to first ask how many tests a student
has taken, then collect all the test scores and calculate the
average score. After displaying the average score, the program also
assigns a letter grade to the student according to the following
grading scale:
Score Grade >=90 A 80-89 B 70-79 C 60-69 D <60 F This program
should perform input validation for test scores and only accept a
positive input value as a score (ask the user to enter another
value if input is invalid). The program should be able to handle
real numbers as test scores and display the average score with two
decimal digits.
The following is a Sample output of the program. How many tests did you take? 3 Enter a Test Score: -80 Error: test score must be positive. Try again 80 Enter a Test Score: 85.5 Enter a Test Score: 92.5 Your Average Score: 86.00 Your Grade: B
I have given comments for each line please go through them to understand the code properly.
no_of_tests_taken = int(input("How many test did you take? ")) # Taking number of tests
if no_of_tests_taken > 0: # checking if number of tests is greater than zero.
test_scores_sum = 0 # variable to keep sum of all the scores taken from user.
loop_counter = 0 # loop counter will only be increased when we have a correct input
while loop_counter < no_of_tests_taken: # loop runs until the loop counter less than number of tests taken
test_score = float(input("Enter a Test Score ")) # taking score from input
if test_score < 0: # if test score is less than zero ask user to enter again the correct score
print("Error: test score must be positive.")
elif test_score > 100: # if test scores cross 100 ask user to enter the score again
print("Error: test score greater than 100")
else:
test_scores_sum += test_score # adding score to test_scores_sum variable
loop_counter += 1 # adding 1 to loop counter
test_average_score = test_scores_sum / no_of_tests_taken # average over the score
print("Your Average Score: {}".format(round(test_average_score, 2))) # rounding the score to two decimals and printing
if test_average_score >= 90: # if average score is great than 90 the grade is "A"
print("Your Grade: A")
elif 80 <= test_average_score < 90: # if average score is between 80 and 90 the grade is "B".
print("Your Grade: B")
elif 70 <= test_average_score < 80: # if average score is between 70 and 80 the grade is "C".
print("Your Grade: C")
elif 60 <= test_average_score < 70: # if average score is between 60 and 70 the grade is "D".
print("Your Grade: D")
elif test_average_score < 60: # if average score is less than 60 the grade is "F"
print("Your Grade: F")
Here while giving grades carefully see the ranges for each of them because in questions 89 to 90 range was not given
the ranges actually would be >= 90 ,
80<=avg< 90 here the upper 90 is open interval means till 89.999 is going to be considered not 90
same goes with the below ranges
70<=avg<80
60<=avg<70
Output for given example:
Another output when the input tests are 4:
please check with different outputs as well and if any doubts please leave comment.