Test Average and Grade
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score.
Write the following functions in the program:
• calc_average—This function should accept five test scores as arguments and return the average of the scores.
• determine_grade—This function should accept a test score as an argument and return a letter grade for the score, based on the following grading scale:
Score Letter Grade
90–100 A
80–89 B
70–79 C
60–69 D
Below 60 F
Test Average and Grade Python code:
#The method 'determine_grade' accept a test score as an argument
#and return a letter grade for the score,
#based on the given grading scale
def determine_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'F'
#The method 'calc_average' should accept five test scores
#as arguments and return the average of the scores.
def calc_average(scores):
total = sum(scores)
average = total / len(scores)
return average
# Prompt to asks the user to enter five test scores
scores = []
for i in range(5):
score = int(input("Enter test score #" + str(i+1) + ": "))
scores.append(score)
# Calculating average
average = calc_average(scores)
# Display a letter grade for each score
# and the average test score.
print("Letter grades:")
for score in scores:
grade = determine_grade(score)
print("Score:", score, "Grade:", grade)
print("Average test score:", average)
Executed Output 1:
Enter test score #1: 80
Enter test score #2: 90
Enter test score #3: 60
Enter test score #4: 99
Enter test score #5: 51
Letter grades:
Score: 80 Grade: B
Score: 90 Grade: A
Score: 60 Grade: D
Score: 99 Grade: A
Score: 51 Grade: F
Average test score: 76.0
Executed Output 2:
Enter test score #1: 50
Enter test score #2: 44
Enter test score #3: 55
Enter test score #4: 60
Enter test score #5: 35
Letter grades:
Score: 50 Grade: F
Score: 44 Grade: F
Score: 55 Grade: F
Score: 60 Grade: D
Score: 35 Grade: F
Average test score: 48.8