Activity: Statistics

Create a file grades.py and copy the following code into it:

# TODO: Finish/fix
def getAverages(myList):
    return 0

# TODO: Finish/fix
def getLowestGrade(myList):
    return 0

# TODO: Finish/fix
def getHighestGrade(myList):
    return 0

# TODO: Finish/fix
def getGrade(score):
    return "F"

# TODO: Finish/Fix
def getGradeDistributionDict(myList):
    grades = {"A": 0, "B": 0, "C": 0, "D": 0, "F": 0}
    return grades


numScores = input("How many scores? ")
numScores = int(numScores)

allScores = []
for i in range(numScores):
    nextScore = 0 # TODO: fill in code to get next score from the user
    
    # TODO: add the nextScore to the list allScores


print("Grades:".rjust(20), allScores )
print("Grade Average:".rjust(20), getAverages(allScores))
print("Lowest Grade:".rjust(20), getLowestGrade(allScores))
print("Highest Grade:".rjust(20), getHighestGrade(allScores))

gradeDistribution = getGradeDistributionDict(allScores)
print("Grade Distribution:".rjust(20), gradeDistribution)
  1. Add code inside the for loop to ask the user to input each score individually as an integer (e.g., the user may type in 100, then 90, then 80, then 98, then 97, pressing enter after each score is input in the shell). Store the result as an integer into nextScore. Hint: remember that the input function returns a string, and int will convert a string to an integer.

  2. Add code to add nextScore to the list allScores, which will contain a list of all integer scores typed in by the user.

  3. Implement the function getAverages so that it computes the average of all scores in the allScores list and returns the average. Do not call the mean function - you are to implement your own. Check that your output is correct (see below).

  4. Implement the functions functions: getLowestGrade(myList) and getHighestGrade(myList) that will return the lowest grade and the highest grade from a given list, respectively. Do not call the min/max function – you are to implement your own function. Check that your output is correct (see below).

  5. Implement a function getGrade(score) which takes an integer score and returns the letter grade “A”, “B”, “C”, “D”, or “F”, using the standard grading scale. Test your function.

  6. Implement a new function: getGradeDistributionDict(gradeList) which will take allScores, which is a list of grades scored from 0 to 100, and the function should return a dictionary where the keys are the strings “A”, “B”, “C”, “D”, and “F” (the grades), and the values associated with each key is the number of scores which have that grade. Hint: use the getGrade(score) function you made in the previous step. Check that your output is correct (see below).

Test your code as you work through each problem

Here is an example of what the shell output will look like when you’re finished. Note the values typed in the shell.

How many scores? 5
Enter score: 100
Enter score: 98
Enter score: 60
Enter score: 81
Enter score: 90
             Grades: [100, 98, 60, 81, 90]
      Grade Average: 85.8
       Lowest Grade: 60
      Highest Grade: 100
 Grade Distribution: {'A': 3, 'B': 1, 'C': 0, 'D': 1, 'F': 0}

How to submit

Submit your working python file to Moodle.