Advertisement
abdullyahuza

classes-and-objects

May 25th, 2022
513
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Student:
  2.     # [assignment] Skeleton class. Add your code here
  3.     def __init__(self, name, age, tracks=[], score=[]):
  4.         self.name = name
  5.         self.age = age
  6.         self.tracks = tracks
  7.         self.score = score
  8.  
  9.     def change_name(self, new_name):
  10.         self.name = new_name
  11.  
  12.     def change_age(self, new_age):
  13.         if type(new_age) is int:
  14.             self.age = int(new_age)
  15.         else:
  16.             self.age = 0
  17.  
  18.     def add_track(self, new_track):
  19.         self.tracks.append(new_track)
  20.  
  21.     def get_score(self):
  22.         return self.score
  23.  
  24.     def __repr__(self):
  25.         return f'Name: {self.name} Age: {self.age} Tracks: {self.tracks} Score: {self.score}'
  26.  
  27. Bob = Student(name="Bob", age=26, tracks=["FE","BE"],score=20.90)
  28.  
  29. # Expected methods
  30. Bob.change_name("Peter")
  31. Bob.change_age('AA')
  32. Bob.add_track("UI/UX")
  33. Bob.get_score()
  34.  
Advertisement
RAW Paste Data Copied
Advertisement