Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKin Yu Wong
1,670 PointsQuestion on the final line of code "print card"
When I follow the code and run the final line "print (card)", I have both the card and location being printed out like below. Why is that?
Add, B1
Add, A3
Boo, B4
Boo, A1
This is my code https://w.trhou.se/e418u19z92
1 Answer
Steven Parker
231,545 PointsThe card object contains an explicit __str__ method which determines what will be shown when it is treated as a string (such as when being printed out). In that method, we can see that the string that it will become is defined as "{self.card}, {self.location}"
, which clearly contains both the name and the location separated by a command and a space.
If you want to see only the name, you could redefine the __str__ method, or you could print(card.card)
in game.py instead of just print(card)
.