Main.py https://pastebin.com/GGMQUsix Student.py https://pastebin.com/DtQKeuzk c
ID: 3812939 • Letter: M
Question
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
It's very common to have to work around poor quality or corrupted data. Note that some lines of data in the text files of this assignment may be corrupted! For example, this line of data does not have enough answers:
N00000003:B,A,D,D,C
... and this line of data has too many answers:
N00000002:C,D,D,C,A,D,A,C,B,D,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D,A,B,C,D,E
… and this line of data has some missing answers:
N00000003:C,A,,D,D,C,,A,B,D,A,B,C,,D
If you encounter a line of data that is un-usable you should print an error message to the user. However, you should try to mark those responses as much as possible.
Here's a sample running of this section for having not enough answers in the data file:
---------------------------------------------------
Enter your choice: 3
---------------------------------------------------
Enter name of the data file: exam5_10_e1.txt
Completed reading of file exam5_10_e1.txt
Not enough answers for ID: N00000003
Sum of marks: 20
...
N00000001: John, john@amail.com, marks: [5]
N00000002: Kelly, kelly@bmail.com, marks: [7]
N00000003: Nicky, nicky@cmail.com, marks: [1]
N00000004: Sam, sam@dmail.com, marks: [2]
N00000005: Adam, adam@amail.com, marks: [5]
Here's a sample running of this section for having too many answers in the data file:
---------------------------------------------------
Enter your choice: 3
---------------------------------------------------
Enter name of the data file: exam5_10_e2.txt
Completed reading of file exam5_10_e2.txt
Too many answers for ID: N00000002
Sum of marks: 19
..
N00000001: John, john@amail.com, marks: [5]
N00000002: Kelly, kelly@bmail.com, marks: [7]
N00000003: Nicky, nicky@cmail.com, marks: []
N00000004: Sam, sam@dmail.com, marks: [2]
N00000005: Adam, adam@amail.com, marks: [5]
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]
You are required to complete the display_summary(marks) function which takes a list of marks and prints the marking summary of the exam. If the fifth option of the menu, i.e., ‘Display the Marking Summary’, is selected, the program should display the marking summary of the exam. The function should compute the following statistics after you've done the marking of the exam:
* the total number of students represented in the data file
* the highest score
* the lowest score
* the average score (mean)
Here's a sample running of this section using the “exam.txt” data file:
...
---------------------------------------------------
Enter your choice: 5
---------------------------------------------------
Grade Summary:
Total number of Students: 5
Highest Score: 6
Lowest Score: 4
Mean: 4.6
Here's a sample running of this section using the “exam2.txt” data file:
...
---------------------------------------------------
Enter your choice: 5
---------------------------------------------------
Grade Summary:
Total number of Students: 4
Highest Score: 7
Lowest Score: 2
Mean: 4.75
Explanation / Answer
from collections import namedtuple
import sys
# Python 2/3 compatibility shim
if sys.hexversion < 0x3000000:
inp, rng = raw_input, xrange # Python 2.x
else:
inp, rng = input, range # Python 3.x
def type_getter(type):
"""
Build a function to prompt for input of required type
"""
def fn(prompt):
while True:
try:
return type(inp(prompt))
except ValueError:
pass # couldn't parse as the desired type - try again
fn.__doc__ = " Prompt for input and return as {}. ".format(type.__name__)
return fn
get_int = type_getter(int)
get_float = type_getter(float)
# Student record datatype
Student = namedtuple('Student', ['name', 'mark'])
def get_students():
"""
Prompt for student names and marks;
return as list of Student
"""
students = []
while True:
name = inp("Enter name (or nothing to quit): ").strip()
if name:
mark = get_float("Enter {}'s mark: ".format(name))
students.append(Student(name, mark))
else:
return students
def main():
cases = get_int("How many cases are there? ")
for case in rng(1, cases+1):
print(" Case {}:".format(case))
# get student data
students = get_students()
# perform calculations
avg = sum((student.mark for student in students), 0.) / len(students)
best_student = max(students, key=lambda x: x.mark)
# report the results
print(
" Case {} average was {:0.1f}%"
" Best student was {} with {:0.1f}%"
.format(case, avg, best_student.name, best_student.mark)
)
if __name__=="__main__":
main()
which runs like:
How many cases are there? 1
Case 1:
Enter name (or nothing to quit): A
Enter A's mark: 10.
Enter name (or nothing to quit): B
Enter B's mark: 20.
Enter name (or nothing to quit):
Case 1 average was 15.0%
Best student was B with 20.0%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.