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 trialdaniel steinberg
14,651 Points[SOLVED] self.value = value or random.randint(1, sides)
please explain the purpose of this line of code.
as I understand it, self.value should be zero sometimes since its an "or", but after running many times I never get a zero:
import random
class Die:
def __init__(self, sides=2, value=0):
if not sides >= 2:
raise ValueError("Must have at least 2 sides")
if not isinstance(sides, int):
raise ValueError("Sides must be a whole number")
self.value = value or random.randint(1, sides)
[MOD: edited code block - srh]
Steve Hunter
57,712 PointsHave you understood the answer to your question?
Steve.
daniel steinberg
14,651 Pointsyes ty!
Steve Hunter
57,712 PointsNo probs!
daniel 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 (not interpreted as false)
Chris Freeman
Treehouse Moderator 68,441 PointsOne way to allow for the value to be 0 you could:
- set default value to negative value
value=-1
- use if statement
if value < 0:
self.value = random.randint(0, sides)
else:
self.value = value
daniel steinberg
14,651 Pointsdaniel steinberg
14,651 PointsPS..sorry for being lazy and not looking at previous questions..I see this has already been answered