SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Value Returning Functions And Modules
Exercise:
Programming Exercises
Question:6 | ISBN:9780132576376 | Edition: 2

Question

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

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.