Avery9115
Avery9115

Reputation: 135

Printing and Formatting an Instance

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)

Upvotes: 2

Views: 117

Answers (4)

hxysayhi
hxysayhi

Reputation: 1987

If I understand you correctly, __str__() may be helpful.

For example:

import random
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)

When you print your Student,you will see:

>>>test_stu = Student("Mike")
>>>print test_stu

My name is Mike. I've been in college for 1 years and I've written 0 programs

And the following links may be helpful:

https://docs.python.org/2.7/reference/datamodel.html?highlight=mro#object.str and Difference between str and repr in Python

Upvotes: 0

Tankobot
Tankobot

Reputation: 1602

You can do this by defining the __str__ method for the class, which is called whenever the instance is converted to a string:

class Student():
     # insert the same init here

    def shout(self):  # we do not need to ask for name because
                      # we already have it stored in self.name
        phrase = "My name is %."
        return phrase % self.name  # format the text before returning it
        # it's also useful to return the string instead
        # of immediately printing it in this case because
        # we will use it later

    def year_at_umich(self):
        phrase = "I've been at umich for %s years"  # do not modify the instance attribute
        return phrase % self.years_UM  # use it to format your phrase

    def say_number_programs(self):  # keeping in pattern with our previous methods
        phrase = "I've written %s programs"
        return phrase % self.programs_written

    def __str__(self):
        return '%s. %s and %s.' % (self.shout(),              # we can format the final string
                                   self.year_at_munich(),     # during the str call
                                   self.say_number_programs)

st = Student('Bob')

print st

This returns

My name is Bob. I've been at umich for 1 year and I've written 0 programs.

Upvotes: 0

Doyousketch2
Doyousketch2

Reputation: 2147

class Student():
  def shout(num, word):
    if num == 1:
      phrase = "My name is %s" % word
    elif num == 2:
      phrase = "I love %s" % word
    elif num == 3:
      phrase = "My favorite food is %s" % word
    return phrase

  def years(num):
    phrase = "I've been at umich for %s years" % num
    return phrase

  def programs(num):
    phrase = "I've written %s programs" % num
    return phrase

Student.shout(1, 'Mud')
'My name is Mud'

Upvotes: 1

shizhz
shizhz

Reputation: 12501

You need to implement method __str__ or __repr__ in class Student. If you just need the instance to be printed in your way, __str__ is enough:

    def __str__(self):
        return "My name is {}. I've written {} programs".format(self.name, self.programs_written)

Upvotes: 0

Related Questions