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 trial

Python Object-Oriented Python (retired) Hack-n-Slash Finishing The Game

Michael Pastran
Michael Pastran
4,727 Points

help with init method and the control flow.

hi, so my question is the following. in the game that we are making close to the end we override the init. my question is not with the init but with the stuff that kenneth wrote in it.

def __init__(self):
    self.setup()

    while self.player.hit_points and (self.monster or self.monsters):
      print('\n'+'='*20)
      print(self.player)
      self.monster_turn()
      print('-'*20)
      self.player_turn()
      self.cleanup()
      print('\n'+'='*20)

    if self.player.hit_points:
      print("You win!")
    elif self.monsters or self.monster:
      print("You lose!")
    sys.exit()

so my question is = for the while loop and the IF statement and ELIF statements. aren't we supposed to test a condition? so for example in the while loop.

    while self.player.hit_points and (self.monster or self.monsters):

is that supposed to be read,

while self.player.hit_points == True and (self.monster == True or self.monsters == True)

if so, does that mean that when self.player.hit_points comes back False, that the player has no more hit points??

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

There is a shorthand used in coding where objects have a "Truthiness", that is, they are considered True if they are not None, not 0, not empty (like {} or []). Therefore you don't have to compare them to "True". More in the docs Truth Value Testing

Since self.player.hit_points is a numeric value:

    while self.player.hit_points:
    # is equivalent to:
    while self.player.hit_points != 0:

For self.monster and self.monsters, an object existing is considered "True".

As another example:

    if list_of_values:
    # is preferred over
    if len(list_of_values) > 0:
Michael Pastran
Michael Pastran
4,727 Points

does it by any chance also have to be with True = 1, False= 0?? thank you btw that was really helpful. it makes a lot more sense now

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Sort of, it's more that 0 or 0.0 is interpreted as False in a Boolean context, and non-0 or non-0.0 float is interpreted as True in a Boolean context. It helps to keep in mind == means is equivalent not IS. is implies there are the exact same thing as would be seen by id(0) == id(False)