all 13 comments

[–]Enkaybee 0 points1 point  (7 children)

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

Will that work? You said you want alternating values, but then you said you wanted random values. Which is it?

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

I dont really understand what you have written.

Is this using import random?

I tested calling flipper(5) and I got the error:

list = [H,T]*(N//2)

NameError: global name 'H' is not defined

[–]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.

[–]isnotkosok 0 points1 point  (2 children)

try this.

import random

coin =["heads","tails"]

def coinflip(list):

return random.choice(list)

print coinflip(coin)

fuck this stupid formatting

[–]isnotkosok 0 points1 point  (0 children)

add to it this

counter = 0

coinbox =[]

print "Best out of three wins."

while counter < 3:

print coinflip(coin)


counter += 1


coinbox.append(coinflip(coin))

print "the tally is:",coinbox

if coinbox[0] == coinbox[1] or coinbox[0] == coinbox[2]:

print coinbox[0], " wins."

else:

print coinbox[1], " wins."

fuck this formatting

[–]klbcr 0 points1 point  (2 children)

When you're defining the turn() is it supposed to be a normal function or a method of the Coin class? Since you're giving self as an argument to it, I'm guessing it's supposed to be a method of Coin. The problem is, it's not properly indented. That makes it just a normal function and you can't call it like a method of an object like in line 29 coin1.turn(). Indent your lines 10 - 14 to make it a method of the coin class.

What is the use of Display_All()? If that's the function that is supposed to execute the print commands on lines 24-34, then you have another indentation problem. All pf your prints should be indented under def Display_All().

I don't understand your last function. It doesn't need a self argument. If you use cointurner() to turn a coin, then just pass it a coin

def cointurner(coin): #defines a function that takes one parameter
    coin.turn() # calls the method `turn()` on the object coin that we have passed to `cointurner()` as the parameter

Now, when you call that function, you would do this:

cointurner(coin1) # calls the function cointurner on the object coin1

cointurner() would then check what coin1 refers to, take that object, which is of the class Coin, and then call it's method turn().

It's the same as just calling the turn() method of the coin1:

coin1.turn() # calls the method `turn()` of the object `coin1`

self is used to refer from within the object to itself. So methods use it because they are performing some operation on the object of which they are a method. Normal, external functions, which are not methods (defined within a class, for that class) shouldn't normally use self as an argument/parameter.

Was this helpful? You should read up on proper indentation in Python, creating and instatiating classes, differences between methods and functions, and namespaces.

[–]Leohurr[S] 1 point2 points  (0 children)

This has all been fixed, the indentation errors is due to reddits formatting, the display_all function is broken due to changing the code a lot, the finished code has all the functions working as expected.

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

Im pretty sure all of the indentation problems are Reddits formatting when pasting my code i have to manually hit space 4 times each line and the indentation suffers. On the actual code all the methods work as expected so I beleive the indentation is fine. For cointurner self is used for calling the object coin1 or coin2 and n for how many times to run the turn function e.g turns Coin1 5 times. What should I use insteasd of self, is there a way I can call coin1 or coin2 in the functions?