Question

In: Computer Science

Python: write a program to first ask how many tests a student has taken, then collect...

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

Solutions

Expert Solution

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.


Related Solutions

Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Write a Java program that will first ask the user how many grades they want to...
Write a Java program that will first ask the user how many grades they want to enter. Then use a do…while loop to populate an array of that size with grades entered by the user. Then sort the array. In a for loop read through that array, display the grades and total the grades. After the loop, calculate the average of those grades and display that average. Specifications Prompt the user for the number of grades they would like to...
Write a python program that will ask the user to enter as many positive and negative...
Write a python program that will ask the user to enter as many positive and negative numbers and this process will only stop when the last number entered is -999. Use a while loop for this purpose. Your program should compute three sums; the sum of all numbers, sum of all positive numbers, and the sum of all negative numbers and display them. It should also display the count of all numbers, count of all positive numbers, and count of...
write a python program to do the following: ask a student for 3 coefficients a b...
write a python program to do the following: ask a student for 3 coefficients a b c using the high_school_quizz to display the solutions of a quadratic problem. after, ask the student if he would like another quadratic equation solved If the student answers anything but yes, the program terminates by displaying a good bye message if the student answers yes (any form of yes should be acceptable) , the student is asked for the coefficient again and the resulting...
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
You have been asked to create a python program that will ask the user how many...
You have been asked to create a python program that will ask the user how many tickets the user would like to purchase for graduation. The user will then choose the number of tickets needed and the program will list the cost of the tickets, a service fee, tax, and the total of the purchase. Specifications: The program should only accept whole numbers when receiving input for the number of tickets needed. No calculations should be performed if input is...
Write a Python program that reads an integer and prints how many digits the number has,...
Write a Python program that reads an integer and prints how many digits the number has, by checking whether the number is ≥10,≥100,≥1000, and so on (up to 1,000,000). Your program should also identify if a number is negative.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT