I am trying to Define a str method for this class here so that when an instance of class Student is printed, it prints something of the format: My name is ___. I've been in college for __ years and I've written __ programs. Below the correct phrasing is printing but the correct values are not printing. Any help would be appreciated.
class Student():
def __init__(self, name, years_at_umich=1):
self.name = name
self.years_UM = years_at_umich
self.bonus_points = random.randrange(1000)
self.programs_written = 0
def __str__(self):
return "My name is %s. I've been in college for %d years and I've written %d programs" \
%(self.name, self.years_UM, self.programs_written)
def shout(self, phrase_to_shout):
print phrase_to_shout
def year_at_umich(self):
return self.years_UM
def write_programs(self, progs=1):
self.programs_written += progs
return self.programs_written
*** tests for diffrent values, currently it's only passing the last test for student 4********
class Student_test(unittest.TestCase):
def test_student1(self):
student1 = Student("Lyra")
self.assertEqual(student1.__str__(),"My name is Lyra. I've been at UMich for 1 years and I've written 0 programs.")
def test_student2(self):
student2 = Student("Aisha")
student2.write_programs()
self.assertEqual(student2.__str__(),"My name is Aisha. I've been at UMich for 1 years and I've written 1 programs.")
def test_student3(self):
student3 = Student("Ali",3)
student3.write_programs(4)
self.assertEqual(student3.__str__(),"My name is Ali. I've been at UMich for 3 years and I've written 4 programs.")
def test_student4(self):
student4 = Student("Aja")
student4.write_programs(12)
self.assertEqual(student4.programs_written, 12)
student4.write_programs()
self.assertEqual(student4.programs_written,13)