I have to input a letter grade, example A+,B-,C, etc., and I need python to calculate the numerical average based on the grades entered. My teacher wants us to create the program so that the user has to input 5 letter grades, and then python calculates the numerical average. Please help. I'm so lost.
Asked
Active
Viewed 876 times
-2
-
4We are not here to write your homework assignment for you. Try something. Come back with a question related to code you've tried. – Andy Feb 16 '16 at 03:23
-
1Hint: Read more about Python `dict`s for starters. – Selcuk Feb 16 '16 at 03:42
-
I don't know Python well enough to answer, but I'll tell you the "physics for poets" of what you need to do. As Selcuk said, you'll need to create a dictionary. The dictionary will have a `key` and a `value` ("A":4, "B":3, "C":2, "D":1, "F":0). http://stackoverflow.com/q/8424942/4475605 Whenever you input something and press enter, you'll want to tell it to look up the value for the letter in the dictionary and append (add) it to an array (list). When the list's count is 5, the compute a value and print it. – Adrian Feb 16 '16 at 04:16
1 Answers
0
Here's some working code for the task you need to do. Hopefully, it will help you to get started. But I strongly discourage you from submitting this code to your teacher. Odds are if you are asking for help with this homework, you have not learned many of the concepts used in my solution (and your teacher will notice that). Do yourself a favor and try writing the code yourself. If you still need help, post a more specific question on StackOverflow with the code you tried.
mean = lambda l: float(sum(l))/len(l) if len(l) > 0 else float('nan')
def letter_to_number(grade):
grades = {
'A+': 100,
'A': 96,
'A-': 92,
'B+': 89,
'B': 86,
'B-': 82,
'C+': 79,
'C': 76,
'C-': 72,
'F': 65
}
return grades.get(grade, grades['F'])
def compute_average_grade(grades):
return mean(map(letter_to_number, grades))

pzp
- 6,249
- 1
- 26
- 38