My Python Counter code is inconsistent, can someone fix it?

Viewed 71

I'm a Beginner; I'm trying to make a program in python 3.5 that flips a coin 100 times, then counts the amount of heads and tails from those 100 flips and displays it at the bottom. I got the 100 flips to work, and I also tried to make a counter to count the amount of h/t that landed, but it's inconsistent and wonky, and the counters doesn't count the correct amount. I would love it if some one could help me.

import random

hcounter = 0
tcounter = 0

while True:
    
    flip = ['heads','tails']

    decision = input("Flip a coin 100 times? (y/n): ")

    if decision == 'y':
#for some reason, the range doubles the number; I dont know why, so since I want 100 flips, I put 50 for the range.
        for x in range(0, 50):
            for y in flip:
                print(random.choice(flip))
        
                if random.choice(flip) == flip[0]:
                    hcounter += 1
                elif random.choice(flip) == flip[1]:
                    tcounter += 1
        
        print("--------------------------------")
        print("Heads: ",hcounter,"\nTails: ",tcounter)
        
    elif decision == 'n':
        print("\nOk")
        break

yeah

4 Answers

This is a really good start to coding! A few things to fix, let's go through 'em.

                if random.choice(flip) == flip[0]:
                    hcounter += 1
                elif random.choice(flip) == flip[1]:
                    tcounter += 1

This isn't correct. If it isn't a head, it is a tail, but you call random.choice(flip) again, asking Python if it is now a tail. If it goes tails then heads, we don't add anything.

        for x in range(0, 50):
            for y in flip:

The first line will make the program run 50 times. But then in each of those 50 loops, the for y will make it run 2 times, once with y as heads and once for y as tails. We don't need this at all.

Apart from that, good job!

import random

hcounter = 0
tcounter = 0

while True:
    
    flip = ['heads','tails']

    decision = input("Flip a coin 100 times? (y/n): ")

    if decision == 'y':
        for x in range(0, 50):
    
            if random.choice(flip) == flip[0]:
                print("Heads!")
                hcounter += 1
            else:
                print("Heads!")
                tcounter += 1
        
        print("--------------------------------")
        print("Heads: ",hcounter,"\nTails: ",tcounter)
        
    elif decision == 'n':
        print("\nOk")
        break

The doubling comes from this:

for x in range(0, 50):  # 50 loops
        for y in flip:  # each with two more operations

An easier way to do this would be something like

flip = ['heads', 'tails']
results = [ random.choice(flip) for i in range(100)]
head_counter = results.count(flip[0])
tail_counter = results.count(flip[1])

If you want to practice incremental counting:

flip = ['heads', 'tails']
results = []
heads, tails = 0, 0
for _ in range(100):
    results.append(random.choice(flip))
    if results[-1] == flip[0]:
       heads += 1
    else:
       tails += 1
enter code here

You can also do that without a for-loop:

k = 100 # times 
hcounter = sum(random.choices([0,1],k=k)) # or tails if you want :)
print("Heads: ", hcounter,"\nTails: ", k - hcounter ) 

Your code is great... except for one part. There is a unnecessary line of code in there. You mentioned doubling correct? Well that's were it comes from. I suggest take of the line of code: for y in flip: You see what this is doing is incrementing through you heads and tails list and size the len of that list is two it doubles. If it was three it would triple. So if you take it off it should only do it once. You code would look like this. Tell me if it works:

import random

hcounter = 0
tcounter = 0

while True:
    
    flip = ['heads','tails']

    decision = input("Flip a coin 100 times? (y/n): ")

    if decision == 'y':
#for some reason, the range doubles the number; I dont know why, so since I want 100 flips, I put 50 for the range.
        for x in range(0, 50):
            print(random.choice(flip))
    
            if random.choice(flip) == flip[0]:
                hcounter += 1
            elif random.choice(flip) == flip[1]:
                tcounter += 1
    
        print("--------------------------------")
        print("Heads: ",hcounter,"\nTails: ",tcounter)
        
    elif decision == 'n':
        print("\nOk")
        break
Related