Newbie Program

E

Eric

I am reading a book on Python and ran across and exercise that I just
can't seem to figure out. Its pretty simple, but I just can't get
past a certain point.

The task is to create a program that flips a coin 100 times and keeps
track of the total of heads and tails which is printed to the screen.

My plan was to use import random with a range of 2 and use a
conditional statement to count to 100. The problem I am encountering
is how do I keep track of the totals for heads and tails?

Any suggestions would be appreciated. Keep in mind I am very new to
Python so there may be a more sophisticated way, but I am just trying
to use what the book has taught so far.

Thanks,
Eric
 
D

Devan L

Eric said:
I am reading a book on Python and ran across and exercise that I just
can't seem to figure out. Its pretty simple, but I just can't get
past a certain point.

The task is to create a program that flips a coin 100 times and keeps
track of the total of heads and tails which is printed to the screen.

My plan was to use import random with a range of 2 and use a
conditional statement to count to 100. The problem I am encountering
is how do I keep track of the totals for heads and tails?

Any suggestions would be appreciated. Keep in mind I am very new to
Python so there may be a more sophisticated way, but I am just trying
to use what the book has taught so far.

Thanks,
Eric

Someone else read the same book, I suppose.

http://groups-beta.google.com/group...b5b0d776d?q=coin+flip&rnum=3#c7ff2d2b5b0d776d
 
D

Dennis Lee Bieber

The task is to create a program that flips a coin 100 times and keeps
track of the total of heads and tails which is printed to the screen.
Continuous update, or just at the end?
My plan was to use import random with a range of 2 and use a
conditional statement to count to 100. The problem I am encountering
is how do I keep track of the totals for heads and tails?
Well, the number of tails has to be (flips - heads), unless you
allow a coin to stand on edge, so you only really have to count heads.
Any suggestions would be appreciated. Keep in mind I am very new to
Python so there may be a more sophisticated way, but I am just trying
to use what the book has taught so far.

import random
hds = sum([random.randint(0,1) for x in xrange(100)])
print "Heads %s, Tails %s" % (hds, 100-hds)

Now that this is out of the way... How about rolling 100 sets of
2D6 dice, and printing out the number of times each sum from 2..12 comes
up? <G>

--
 
?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

Eric said:
I am reading a book on Python and ran across and exercise that I just
can't seem to figure out. Its pretty simple, but I just can't get
past a certain point.

The task is to create a program that flips a coin 100 times and keeps
track of the total of heads and tails which is printed to the screen.

My plan was to use import random with a range of 2 and use a
conditional statement to count to 100. The problem I am encountering
is how do I keep track of the totals for heads and tails?

Any suggestions would be appreciated. Keep in mind I am very new to
Python so there may be a more sophisticated way, but I am just trying
to use what the book has taught so far.

Thanks,
Eric
.... cnt[random.choice(coin)] += 1
....
my solution
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top