Python 3.5 Main.py https://pastebin.com/GGMQUsix Student.py https://pastebin.com
ID: 3813571 • Letter: P
Question
Python 3.5
Main.py
https://pastebin.com/GGMQUsix
Student.py
https://pastebin.com/DtQKeuzk
classlist.txt:
https://pastebin.com/aHJ7yHma
exam.txt:
https://pastebin.com/ZjKPsyv7
exam2.txt:
https://pastebin.com/QkDgAK2g
solution.txt:
https://pastebin.com/wHTfWYzV
All changes must be made to Main.py
You are required to complete the display_result(claslist) function which takes a class list and prints the marking result for all students in the class list. If the forth option of the menu, i.e., ‘Display the Marking Result’, is selected, the program should display the marking result.
Here's a sample running of this section using the “exam.txt” data file:
...
---------------------------------------------------
Enter your choice: 4
---------------------------------------------------
N00000001: John, john@amail.com, marks: [4]
N00000002: Kelly, kelly@bmail.com, marks: [5]
N00000003: Nicky, nicky@cmail.com, marks: [4]
N00000004: Sam, sam@dmail.com, marks: [6]
N00000005: Adam, adam@amail.com, marks: [4]
Here's a sample running of this section after TWO exams have been marked:
...
---------------------------------------------------
Enter your choice: 4
---------------------------------------------------
N00000001: John, john@amail.com, marks: [4, 5]
N00000002: Kelly, kelly@bmail.com, marks: [5, 7]
N00000003: Nicky, nicky@cmail.com, marks: [4]
N00000004: Sam, sam@dmail.com, marks: [6, 2]
N00000005: Adam, adam@amail.com, marks: [4, 5]
Explanation / Answer
N00000001: John, john@amail.com, marks: [4, 5]
N00000002: Kelly, kelly@bmail.com, marks: [5, 7]
N00000003: Nicky, nicky@cmail.com, marks: [4]
N00000004: Sam, sam@dmail.com, marks: [6, 2]
N00000005: Adam, adam@amail.com, marks: [4, 5]
def getLetterGrade(score):
score = round(score)
if score >= 90: return "A"
if 90 > score >= 80: return "B"
if 80 > score >= 70: return "C"
if 70 > score >= 60: return "D"
if 60 > score: return "F"
While it's OK and very readable, I have two points of criticism:
an if...elif..else should be used in this situation, because if the score is over 90, the current code will still check
all the other if statements, too. It's a very tiny overhead, totally negligible unless it is done a million times in a row,
but still, it would be cleaner to use if...elif...else, because that only evaluates the comparison statements until it
finds one that is True.
While this code is very readable, I wondered if there's a more mathematical solution, one that makes use of the fact that
grades are assigned on a linear scale. So I came up with using the list index:
["F","D","C","B","A"][0] == "F"
["F","D","C","B","A"][1] == "D", etc.
So we just need a formula that converts values below 60 to 0, those from 60 to 69 to 1, and so forth. This is easily
accomplished. Provided that score is rounded to the next integer, (score - 50) / 10 will return 3 (the list index of "B")
for precisely the values 80 to 89. Now we only need to make sure that the score does not go above 99 or below 50, because
otherwise we would end up calculating invalid list indexes.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.