In: Computer Science
How would I setup this dictionary for Python 3?
class Student(object):
def __init__(self, id, firstName, lastName, courses = None):
The “id”, “firstName” and “lastName” parameters are to be directly assigned to member variables (ie: self.id = id)
The “courses” parameter is handled differently. If it is None, assign dict() to self.courses, otherwise assign courses directly to the member variable.
Note: The “courses” dictionary contains key/value pairs where the key is a string that is the course number (like “course1”) and the value is a number from 0-4.0 (represents the grade the student received).
Example of the input:
123456, John, Wick, 'Course1': 3.50, 'Course2': 3.00, 'Course21': 4.00, 'Course22': 3.75, 'Course25': 4.00
class Student(object): def __init__(self, id, firstName, lastName, courses = None): self.id = id self.firstName = firstName self.lastName = lastName if courses is None: self.courses = dict() else: self.courses = courses def printMe(self): print(self.id) print(self.firstName) print(self.lastName) print(self.courses) stud = Student(123456, 'John', 'Wick', {'Course1': 3.50}) stud.printMe()
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.