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()

Recommended Answers

All 14 Replies

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()
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 :)

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?

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...

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

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

What does your code look like?

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!

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*

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.

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.

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.

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.

Thanks for the information :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.