Python Forum

Full Version: [split] Coin Flip Program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,
I don't know if I should be adding my issue to this thread or starting a new one. I too am having trouble with my coin flip/toss game but my issue is a little different.
I was asked to create a game that 'tosses' a coin 100 times and then displays the results for how many times it landed on each side. I seem to be able to get the game to work in that it's tossing it 100 times and displaying the results, but my problem is that it doesn't seem very random. All I've been able to get is 100 heads or 100 tails. Never any mix of the 2. I've messed around with this and tried several different things such as changing the range of numbers, i.e.

toss < 2 instead of toss == 1

I tried setting the randint range from 1,2 to 0,1

I even tried expanding the range to 1, 10 and using if toss < 5, then heads +=1

Nothing I have tried seems to alter my results. I've been racking my brain trying to figure this out and I can't. I've searched through the help files in python on the random module and tried to search on google for more info. Basically I'm trying to let you all know that I've done all I can to find the answer on my own before coming to ask you. As always I appreciate any input you can offer.
below are my code and output examples

import random

toss = random.randint(1,2)
heads = 0
tails = 0
count = 0

print(
'''
                Welcome to Coin Toss!
We\'ll flip a coin 100 times and tell you the results.

'''
)
input('\nPress the enter key to continue.')
while count != 100:
    count += 1
    if toss < 2:
        print('You got heads!')
        heads += 1
        
    else:
        print('You got tails!')
        tails += 1

print('After 100 tosses?...')
print('\nYou got heads', heads, 'times!')
print('\nand You got tails', tails, 'times!')
print('\nThanks for playing Coin Toss!')

input('\n\nPress the enter key to exit.')
Output:
You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! After 100 tosses?... You got heads 0 times! and You got tails 100 times! Thanks for playing Coin Toss! Press the enter key to exit.
AND

Output:
You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! You got heads! After 100 tosses?... You got heads 100 times! and You got tails 0 times! Thanks for playing Coin Toss! Press the enter key to exit.
Don't hijack threads by other members.

your problem is that you toss only once at the beginning. you never toss a coin in the loop.
Wow! Hijacking sounds harsh. I wasn't sure and in fact I was just on the chat link to ask if it was better to start a new thread instead of adding my issue to an existing one. Now I know, so thanks.

Please forgive my ignorance but if it's not looping, how is it printing the same thing 100 times??
it IS looping, but you never change the value of toss in the body of the loop
'hijacking thread' is not harsh, but common expression meaning someone post his own question in other member's thread. this makes difficult to follow original thread/topic as both (original and new one) get mixed so one may get loss who is answering what question...
Initially I was concerned about adding duplicate issues and flooding the forum with unneccessary info. After posting it I realized that seemed wrong and was in the process of clarifying on the chat room when you split it off for me. Sorry for my error and appreciate the education.

Also I see now what you meant about the loop and the toss variable, I think. So to get that to loop correctly I need to find a way to add the toss variable to loop and still allow it to do that 100 times. That helps and I will give it some thought.

Thanks for the help, I truly appreciate it.
(Sep-24-2017, 09:42 AM)buran Wrote: [ -> ]it IS looping, but you never change the value of toss in the body of the loop

I don't know why my brain tries so hard to make things harder than they need to be. I was already starting to have the realization that it must have something to do with the toss and my loop, but then you confirmed it. After some trial and error, ultimately all I did was add the same line toss = random.randint(1,2) to the loop itself and Boom! problem resolved. For some reason it didn't occur to me that I needed to add that line inside the loop or that I even could add it to get it to work.

Here are new examples.

import random

toss = random.randint(1,2)
heads = 0
tails = 0
count = 0


print(
'''
                Welcome to Coin Toss!
We\'ll flip a coin 100 times and tell you the results.

'''
)
input('\nPress the enter key to continue.')
while count != 100:
    count += 1
    toss = random.randint(1,2)

    if toss < 2:
        print('You got heads!')
        heads += 1
        
    if toss > 1:
        print('You got tails!')
        tails += 1

print('After 100 tosses?...')
print('\nYou got heads', heads, 'times!')
print('\nand You got tails', tails, 'times!')
print('\nThanks for playing Coin Toss!')

input('\n\nPress the enter key to exit.')
Output:
You got heads! You got heads! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got heads! You got heads! You got tails! You got tails! You got heads! You got tails! After 100 tosses?... You got heads 47 times! and You got tails 53 times! Thanks for playing Coin Toss! Press the enter key to exit.
Thanks again for the help!! Smile
One day I hope to be able to assist someone else rather than always be the one asking for help.