Search code examples
pythoncoin-flipping

Why is Python not printing out the correct answers?


I cant understand why Python is not printing out how many heads and tails there have been in my coin toss game? and is consistently printing out 0, 100

# part 2
import random
heads = 0
tails = 0

flip_coin = ['heads', 'tails']

while (heads + tails) < 100:
    flip = random.choice(flip_coin)
    if flip == heads:
        heads += 1
    else:
        tails += 1
print(heads)
print(tails)

any ideas?


Solution

  • You did a mistake here

    if flip == heads: # You are comparing with the heads count integer variable
    

    What you want is

    if flip == 'heads': # You want to know if it's the string 'heads'
    

    and is consistently printing out 0, 100

    Because of that mistake above, if you analyze that if else, because flip == heads was always false, you always get have the else: tails += 1 executed, hence tails = 100 and heads = 0