Scientific Computing
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
# TODO: Finish/Fix
def getMostOftenGrade(myDictionary):
return ""
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)
print("Most Often Grade:".rjust(20), getMostOftenGrade(gradeDistribution))
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.
Add code to add nextScore
to the list allScores
, which will contain a list of all integer scores typed in by the user.
Implement the function getAverages
so that it computes the average of all scores in the allScores
list and returns the average. Check that your output is correct (see below).
Implement the functions functions: getLowestGrade(myList)
and getHighestGrade(myList)
that will return the lowest grade and the highest grade from a given list, respectively. Check that your output is correct (see below).
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.
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).
Implement a new function getMostOftenGrade(gradeDictionary)
which takes a dictionary as a parameter (as constructed from the previous function). It will compute and return the letter grade which is most often occuring (arbitrarily breaking ties). Check that your output is correct (see below).
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}
Most Often Grade: A
Submit your working python file to Moodle.