[Tutor] Use __str__ method for int values
Jason Friedman
jsf80238 at gmail.com
Sun Mar 10 21:31:17 CET 2013
> I am trying to use a __str__ method to display the values of attribute mood
> = self.hunger + self. boredom.
> When I try to execute I get this error:
>
> Traceback (most recent call last):
> File "C:/Users/Vincent/Documents/Programming Tutorials/Python Programming
> for the Absolute Beginner - Project
> Files/source/chapter08/critter_caretaker_vpb3.py", line 105, in <module>
> main()
> File "C:/Users/Vincent/Documents/Programming Tutorials/Python Programming
> for the Absolute Beginner - Project
> Files/source/chapter08/critter_caretaker_vpb3.py", line 99, in main
> print(crit)
> TypeError: __str__ returned non-string (type int)
> def __str__(self):
> mood = self.boredom + self.hunger
> return mood
I believe the problem is that your __str__ method returns an int
rather than a string.
Try:
mood = str(self.boredom + self.hunger)
More information about the Tutor
mailing list