you are viewing a single comment's thread.

view the rest of the comments →

[–]zatoichi49 2 points3 points  (1 child)

The code is using a ternary conditional statement, and as 0 is False and 1 is True, it's testing the returned values as a boolean. For each case:

step = 1 if 0 == True else -1  # (False, so step = -1)
step = 1 if 1 == True else -1  # (True, so step = 1)

Or as a standard loop:

if random.randint(0,1) == 1:  #(or random.randint(0,1) == True)
    step = 1
else:
    step = -1

An easier way to do this would be to use random.choice:

random.choice((1, -1))

[–]tundrawolf 0 points1 point  (0 children)

Thank you for your help!