I can't manage to get this __str__ to work.
I created a class,
class Hangman :
and then
def __str__(self) :
return "WORD: " + self.theWord + "; you have " + \
self.numberOfLives + "lives left"
there's an init statement and assignment in the program but I can't get the thing to work!
the only way I can do it is by doing this, but surely what's the point of using __str__
def __str__(self) :
print("WORD: {0}; you have {1} lives left".\
format(self.theWord,self.numberOfLives))
Code:
theWord = input('Enter a word ')
numberOfLives = input('Enter a number ')
hangman = Hangman(theWord,numberOfLives)
Hangman.__str__(hangman)
Output:
>>>
Enter a word Word
Enter a number 16
>>>
using the print method, output:
>>>
Enter a word word
Enter a number 16
WORD: word; you have 16 lives left
>>>