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 trialKailash Seshadri
3,087 Pointsreturn 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!
1 Answer
Josh Keenan
20,315 PointsYou 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
3,087 PointsYep 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?
Kailash Seshadri
3,087 PointsKailash Seshadri
3,087 PointsPlaying around with returning 2 variables does clarify a little and these as the inputs and outputs I got:
When both a and b are
True
the output isTrue
. When one of them, or both, areFalse
, the output isFalse
. When we use strings and integer pairs, and try mixingTrue
/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 returningTrue
andTrue
givesTrue
, but any other combination givesFalse
?