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 trialHenry Lin
11,636 Pointswhy is self.value = value or random.randint(1,sides) instead of self.value = random.randint(1,sides) directly?
The sides = "face" in a die The value = "face showing up" after rolling a die. We don't know what value we are going to get until we see the face showing up. Therefore, what is the point that we pass a value here? Why do we want to set up a value?
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsGood question! He mentions it quickly at 1:55 in the video: "Let's let them set a value, just in case they want the die to be four or whatever for testing purposes.
"
The value
might or might not be passed in. If not passed in it will get the default value of 0. Now to the statement in question. The or
makes the right-hand side of the statement evaluate each expression, returning the first "truthy" value. If not passed in, value
will be 0 (not truthy), so the randint
is then evaluated and assigned to self.value
. If a non-zero value
is passed in then this value will be assigned to self.value
and the randint
expression will not be evaluated.
Post back if you need more help!!
David Luo
1,215 PointsWow, never knew you can do that. Setting something value = x or y. So if x is "False" or "None" or not "truthy" then it takes in y and sets it to value? Is that how it works?
Chris Freeman
Treehouse Moderator 68,441 PointsCorrect. See truth value testing and specifically and, or, not in the docs.
daniel steinberg
14,651 Pointsdaniel steinberg
14,651 Pointswhat if you actually wanted a value of zero to be valid and not false, for ex..if you roll a seven and want the value to be zero.