954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

coin flip

I need to have 4 functions which asks the user how many times to flip the coin and then displays the number of heads and the number of tails as a decimal?

def getInputs():
    times=input("please enter the number of times the coin should be flipped: ")

def simulateFlips():
    for i in range(times):
        if randint(1,2) == 1:
            print "Heads"
        else:
            print "Tails"
def main():
    getInputs()
main()

def displayResults():
    simulateFlips()
gangster88
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
import random

def main():
    times = getInputs()
    heads, tails = simulateFlips(times)
    displayResults(heads, tails)


def getInputs():
    times = int(raw_input("please enter the number of times the coin should be flipped: "))
    return times


def simulateFlips(times):
    heads = 0
    tails = 0
    for i in range(times):
        if random.randint(1,2) == 1:
            heads += 1
        else:
            tails += 1

    return (heads, tails)


def displayResults(heads, tails):
    print 'Heads: %d \nTails: %d' % (heads, tails)

if __name__ == '__main__':
    main()
ShadyTyrant
Junior Poster
127 posts since Nov 2009
Reputation Points: 10
Solved Threads: 19
 
import random

def main():
    times = getInputs()
    heads, tails = simulateFlips(times)
    displayResults(heads, tails)


def getInputs():
    times = int(raw_input("please enter the number of times the coin should be flipped: "))
    return times


def simulateFlips(times):
    heads = 0
    tails = 0
    for i in range(times):
        if random.randint(1,2) == 1:
            heads += 1
        else:
            tails += 1

    return (heads, tails)


def displayResults(heads, tails):
    print 'Heads: %d \nTails: %d' % (heads, tails)

if __name__ == '__main__':
    main()

This is similar to what i thought. What would you alter though if you wanted to display the results as a Heads or Tails probability of 1.0? Say the user entered 100 flips, and instead of returning 55 and 45, it would return it as 0.55 and 0.45. Thanks for any help in advance, ive been looking at this problem for over an hour now and i still cant work out how to work out the heads and tails probability.

PS. Please dont be clever and just stick a "0." in the print statement, as this would only "work" if the user entered 100 in the first place...it must be a float probability of 1 no-matter what the user enters, thanks :)

Willz.
Newbie Poster
3 posts since Nov 2009
Reputation Points: 10
Solved Threads: 2
 

So you just want to output the percentage calculation for heads and tails?

Wouldn't you just divide the number collected by the total sample size?

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

So you just want to output the percentage calculation for heads and tails?

Wouldn't you just divide the number collected by the total sample size?


Yes the answer was staring at me in the face, i just divided Heads and Tails by "times" x multiplied by 100...

Willz.
Newbie Poster
3 posts since Nov 2009
Reputation Points: 10
Solved Threads: 2
 

I have learned most of what I know by:

* Listening to others that know more than me
* Screwing up
* Trying new things until something works

But not necessarily in that order! Not giving up is required.
Keep it up Andy. The light will come on one day.
SNIP

melina386
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

Hmm. I'm doing something similar to a coin flipping program and i can't get the 'times' variable to be defined when its used in the results function. Im not sure why this is! I think it has something to do with it being returned? :o

charlottetemp
Newbie Poster
11 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
 

What does your code look like?

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

charlottetemp, can you post the code? Not sure if it needs to be a in a new thread to avoid hijacking this one...

edit: the almighty vegaseat beat me by seconds!

Mensa180
Light Poster
31 posts since Oct 2009
Reputation Points: 13
Solved Threads: 7
 
def main():
    times = getInputs()
    heads, tails = simulateFlips(times)
    displayResults(heads, tails)
   

def getInputs():
    times = int(raw_input("please enter the number of times the coin should be flipped: "))
    return times

def simulateFlips(times):
    import random
    heads = 0
    tails = 0
    for i in range(times):
        if random.randint(1,2) == 1:
            heads += 1
        else:
            tails += 1
        return (heads, tails)

def displayResults(heads, tails):
    print (heads/times * 100 ),"Heads Probability."
    print (tails/times * 100), "Tails Probability."

main()


Just using this code as a basis just unsure of the discussion that occurred in here about how to get probability. didn't work for me :O! *was an error unable to read times*

charlottetemp
Newbie Poster
11 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
 

Well, you are almost there. A few minor changes, see the comments in the modified code ...

def main():
    times = getInputs()
    heads, tails = simulateFlips(times)
    displayResults(heads, tails, times)  # added times to args
   

def getInputs():
    times = int(raw_input("please enter the number of times the coin should be flipped: "))
    return times

def simulateFlips(times):
    import random
    heads = 0
    tails = 0
    for i in range(times):
        if random.randint(1,2) == 1:
            heads += 1
        else:
            tails += 1
    return (heads, tails)  # moved return out of the for loop

def displayResults(heads, tails, times):
    print heads, tails, times  # for testing only
    print (100.0*heads/times),"Heads Probability."   # changed formula a little
    print (100.0*tails/times), "Tails Probability."

main()


Note that a temporary test print is a good idea for trouble shooting. Also in Python2 something like 8/10 is an integer division and will give a zero, introducing a float like 8.0/10 will make it a floating point division. In Python3 this has been updated such that/ is a floating point division and // is an integer division.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This function returns on the first pass through the loop.

def simulateFlips(times):
    import random
    heads = 0
    tails = 0
    for i in range(times):
        if random.randint(1,2) == 1:
            heads += 1
        else:
            tails += 1
        return (heads, tails)

## it should be
    return heads,  tails


Also you have to convert to a float when dividing
print (float(heads)/times*100), "Heads Probability."
and pass "times" to the function displayResults.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

I'm still pretty new to python and haven't figured out the scope problem of local and global time yet, however when I make it just print heads and tails after it was supposed to calculate flips it gives me 0 and 1 no matter what. I'm guessing this stems from the fact def simulateFlips(times) isn't getting the correct amount of times to flip the coin.

Mensa180
Light Poster
31 posts since Oct 2009
Reputation Points: 13
Solved Threads: 7
 

The return statement is still in the wrong place. You return from the function after the first flip. See Vegaseat's or my previous post.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Thanks for the information :)

charlottetemp
Newbie Poster
11 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: