0

[Python] Need help in implementing error handling in code

I have written a code that collects student's assignment/exam results and puts into a list of student dictionary object. Within the code there is also a dictionary that consist of the weightage of each assignment or exam. This then allows me to calculate the weighted results. How can I implement error handling into this code so that an error can be raised if weights dictionary contains entries that don't match the ones stored in the student dictionary? For example: Student Dict: A1, A2, A3 Weights: A1, E1 (Error is raised since E1 isn't present) [Current code] class Student: # Part 1a: Creating student class def __init__(self, stud_dict): self.name = stud_dict['name'] self.results = stud_dict['results'].copy() # Part 2: Getting weighted result def get_weighted_result(self, weights): result = 0 for key in weights: result += weights[key] * self.results[key] return result # Part 1b: Converting student dictionary list to student object list def dict_to_class_obj(stud_dicts): students = [] for stud_dict in stud_dicts: students.append(Student(stud_dict)) return students #Test Section stud_dicts = [ { "name": "Fus Ro Dah", "results": { "assignment_1": 10, "assignment_2": 10, "examination_1": 10, } }, { "name": "Foo Barry", "results": { "assignment_1": 1, "assignment_2": 2, "examination_1": 3, } }, ] # creating Student objects list students = dict_to_class_obj(stud_dicts) print(students[0].name) print(students[0].results) print(students[0].get_weighted_result({"assignment_1": 1.0, "examination_1": 9.0}))

16th Nov 2020, 3:13 PM
Frost Spectre
Frost Spectre - avatar
4 Respuestas
+ 2
Hi Frost Spectre, I think your chances for answer will improve if you save the code in SoloLearn and share the saved code link instead. Raw text code is not as easy to inspect (must be copy/pasted all the time). Follow the below guide to sharing links https://www.sololearn.com/post/74857/?ref=app
16th Nov 2020, 3:50 PM
Ipang
+ 1
And? you can use sql code in python files too!
16th Nov 2020, 3:56 PM
Shadoff
Shadoff - avatar
0
I am required to doing it in python. Its not an actual working database.
16th Nov 2020, 3:54 PM
Frost Spectre
Frost Spectre - avatar
- 2
You do it totally wrong. Why you collect all data inside dictionary variable??? Use sqlite3 and sql queries to add all results of student's assignment to the database table!
16th Nov 2020, 3:50 PM
Shadoff
Shadoff - avatar