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 Instant Objects Method Arguments

Kailash Seshadri
Kailash Seshadri
3,087 Points

return self.sneaky and bool(random.randint(0,1))

In the video, Kenneth uses the code

 return self.sneaky and bool(random.randint(0,1))

instead of the much clearer, and easier to understand

if self.sneaky == True:                 
    return bool(random.randint(0,1))
else: return False

which we have been using since we started learning. How does the return function that he used work? He doesn't explain it in the video, and isnt very obvious when you look at it. Thanks a lot!

Kailash Seshadri
Kailash Seshadri
3,087 Points

Playing around with returning 2 variables does clarify a little and these as the inputs and outputs I got:

def ret(a,b):
   return a and b
ret (True, True)        # Output: True
ret (False, False)      # Output: False
ret (True, False)       # Output: False
ret (True, "False")     # Output: "False"
ret ("False", True)     # Output: True
ret ("True", "False")   # Output: "False"
ret ("True", "True")    # Output: "True"
ret ("False", "False")  # Output: "False"
ret ("False", 3)        # Output: 3
ret (3, "False")        # Output: "False"

When both a and b are True the output is True. When one of them, or both, are False, the output is False. When we use strings and integer pairs, and try mixing True/False values with them, only the second one i.e. b is returned. Is there a specific reason why this doesnt bring out an error, and skips straignt to returning b? Is there a reason why returning True and True gives True, but any other combination gives False?

1 Answer

Josh Keenan
Josh Keenan
19,652 Points

You are venturing into the world of discrete mathematics with this question, which is incredibly important in the field of computer science as it underpins everything.

What you have in the first 3 tests forms most of a truth table, in this case for an AND gate, the only case a statement can be true with two boolean values is with True and True. In plain english, if some statement P and some statement Q are both true, then P ^ Q is True. Any other combination would be false, if P is True, and Q is False, then P ^ Q is False.

If this were an OR gate, then either or both could be True and the result would return False only if P and Q are both false.

In the example Kenneth has, he is using an AND gate to give the user a chance to get a return of True, by using the pre-existing boolean and throwing it at an AND gate with another, all in a single line. His code is trying to be short and efficient, 'Why write more code if you don't need to?' kind of thinking. Hope this helps

Kailash Seshadri
Kailash Seshadri
3,087 Points

Yep this makes perfect sense, thanks a lot Josh!

Any idea why when I use a combination of True/False values with strings and integers, does the second part only get returned instead of an error popping up? Based on what you told me, strings and integers should not be able to pass through and AND Gate, , as it is impossible to say if a string or integer is True or False. In the case that they could, the why isn't the output False as would be expected?