Back to Freecodecamp

Challenge 165: Class Average

curriculum/challenges/english/blocks/daily-coding-challenges-python/694596b0585c11170ac7c7fd.md

latest2.5 KB
Original Source

--description--

Given an array of exam scores (numbers), return the average score in form of a letter grade according to the following chart:

Average ScoreLetter Grade
97-100"A+"
93-96"A"
90-92"A-"
87-89"B+"
83-86"B"
80-82"B-"
77-79"C+"
73–76"C"
70-72"C-"
67-69"D+"
63-66"D"
60–62"D-"
below 60"F"

Calculate the average by adding all scores in the array and dividing by the total number of scores.

--hints--

get_average_grade([92, 91, 90, 94, 89, 93]) should return "A-".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([92, 91, 90, 94, 89, 93]), "A-")`)
}})

get_average_grade([84, 89, 85, 100, 91, 88, 79]) should return "B+".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([84, 89, 85, 100, 91, 88, 79]), "B+")`)
}})

get_average_grade([63, 69, 65, 66, 71, 64, 65]) should return "D".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([63, 69, 65, 66, 71, 64, 65]), "D")`)
}})

get_average_grade([97, 98, 99, 100, 96, 97, 98, 99, 100]) should return "A+".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([97, 98, 99, 100, 96, 97, 98, 99, 100]), "A+")`)
}})

get_average_grade([75, 100, 88, 79, 80, 78, 64, 60]) should return "C+".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([75, 100, 88, 79, 80, 78, 64, 60]), "C+")`)
}})

get_average_grade([45, 48, 50, 52, 100, 54, 56, 58, 59]) should return "F".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([45, 48, 50, 52, 100, 54, 56, 58, 59]), "F")`)
}})

--seed--

--seed-contents--

py
def get_average_grade(scores):

    return scores

--solutions--

py
def get_average_grade(scores):
    avg = sum(scores) / len(scores)

    if avg >= 97: return "A+"
    if avg >= 93: return "A"
    if avg >= 90: return "A-"

    if avg >= 87: return "B+"
    if avg >= 83: return "B"
    if avg >= 80: return "B-"

    if avg >= 77: return "C+"
    if avg >= 73: return "C"
    if avg >= 70: return "C-"

    if avg >= 67: return "D+"
    if avg >= 63: return "D"
    if avg >= 60: return "D-"

    return "F"