Welcome to the Treehouse Community
Looking to learn something new?
What does "if self.sneaky:" mean exactly?
I don't get "if self.sneaky" part in the following code. What value does self.snekay get/have in running the method? I assume self.sneaky value is True, right? Then if self.sneaky is True then randomly it can be called True and False by bool(random.randint(0,1)). If bool is called True then it is True, if bool is False then False...That part I under stand but why it should be run by bool in if condition? I can be just simple self.sneaky is True or not. I don't just get this logic in the code the course taken.
import random class Thief: sneaky = True def pickpocket(self): if self.sneaky: return bool(random.randint(0, 1)) return False
Chris FreemanTreehouse Moderator 67,989 Points
Good question! Here's how I read the following code:
import random class Thief: sneaky = True # class attribute (will exist in all class instances) def pickpocket(self): """method to determine if thief successfully picks a pocket based on sneaky attribute and a bit of luck. """ # if attribute is True, use chance to see if successful if self.sneaky: # return 50-50 chance on being successful return bool(random.randint(0, 1)) # if not returned above, self.sneaky must be false. # so no chance at success. return False (failure) return False
In this code segment,
self.sneaky will always be
True so the
if will always run. In later examples, a
sneaky value will be passed into the
__init__() method where it will be assigned to
self.sneaky thus making the
if check in the
pickpocket method necessary.
Post back if you have more questions. Good Luck!
Euenlee Tan2,165 Points
When Kenneth says, kenneth.sneaky = False, what does this exact line of code mean? Does it mean he assigns False to Kenneth? Also, in the line, if self.sneaky:, shouldn't it be if self.sneaky = True: ? I totally do not understand the last few parts of this video of what Kenneth is actually doing