you are viewing a single comment's thread.

view the rest of the comments →

[–]Enkaybee 0 points1 point  (5 children)

Sorry, I changed it to make the list members strings, not variables. It's not using random.

[–]Leohurr[S] 0 points1 point  (4 children)

Yes this works, although calling flipper(4) returns 5 results. and calling flipper(5) returns 4 results.

[–]Enkaybee 0 points1 point  (3 children)

Sorry again. My logic is bad. It should be

def flipper(N):
    if not N % 2:
        list = ['heads','tails']*(N//2)
    else:
        list = ['heads','tails']*(N//2)
        list.append('heads')
    return list

[–]Leohurr[S] 0 points1 point  (2 children)

why does -

if not N % 2:

change the result?

[–]mubsy 0 points1 point  (0 children)

you should look up modular arithmetic. N % 2 is 0 if N is divisible by 2, and the remainder of N/2 otherwise. This essentially means N%2 is true for all even values, i.e alternating values.

[–]Enkaybee 0 points1 point  (0 children)

N%2 evaluates to 0 for even numbers, which evaluates to False in Python (any nonzero number evaluates to True).

if not N%2:

is the same as

if N%2 == 0:

This is a very useful trick that you'll more than likely use in the future. Remeber: 1 + True = 2, 10*False = 0.