[EMAIL PROTECTED] wrote:
> Thanks to all who replied. I did not ask for other iterations of my
> program. I asked what was wrong with it.
Please understand that usenet is not a commercial support service.
Everyone is free to answer how he likes. Or not to answer at all...
--
bruno desthuilliers
Thanks to all who replied. I did not ask for other iterations of my
program. I asked what was wrong with it. To those who did just that,
explained what was wrong, thank you for answering my question.
--
http://mail.python.org/mailman/listinfo/python-list
It may be shorter but it keeps the entire list in memory and has to
iterate over the list twice!
Does he/she need the entire list?
import random
heads = 0
flips = 100
for i in xrange(flips):
if random.randint(0,1): heads += 1
print "Heads:%s" % heads
print "Tails:%s" % (flips - heads)
"D
import random
flips = 100
results = [random.randint(0,1) for i in range(flips)]
heads = results.count(0)
tails = results.count(1)
print "Heads:%s" % heads
print "Tails:%s" % tails
I think this is more compact.
--
http://mail.python.org/mailman/listinfo/python-list
db wrote:
> On Tue, 28 Jun 2005 00:23:30 -0700, ChuckDubya wrote:
>>... if flips >= 99:
>>print "Heads: " + heads
>>print "Tails: " + tails
>>print "Total: " + flips + "flips"
>>raw_input("Press the enter key to exit.")...
>
> Your programm gives an error. You are trying to
[EMAIL PROTECTED] wrote:
> ##Coin Flip: randomly flips 100 "coins" and prints results
> ##Original draft: june 27, 2005
> ##Chuck
>
> import random
> heads = 0
> tails = 0
> flips = 0
> while flips < 99:
> coin = random.randrange(0, 2)
> if coin == 0:
> heads = heads +
Hi,
About the error, you already got the answer from the "experts".
Beeing almost a newbie, I tried instead an elaboration of your example,
using lists.
Furthermore I timed the two methods and to my surprise the "list
method" takes longer:
# Head_Tail.py
import random, time
nStart= time.time()
#
On Tue, 28 Jun 2005 00:23:30 -0700, ChuckDubya wrote:
> ##Coin Flip: randomly flips 100 "coins" and prints results
> ##Original draft: june 27, 2005
> ##Chuck
>
> import random
> heads = 0
> tails = 0
> flips = 0
> while flips < 99:
> coin = random.randrange(0, 2)
> if coin == 0:
[EMAIL PROTECTED] writes:
> When I save and run this "program", I get a DOS window that flashes at
> me and disappears. What's wrong with it?
You can't just click the file.py icon and expect a program like that
to do something reasonable. There are two ways to deal with it:
1) start a DOS box a
##Coin Flip: randomly flips 100 "coins" and prints results
##Original draft: june 27, 2005
##Chuck
import random
heads = 0
tails = 0
flips = 0
while flips < 99:
coin = random.randrange(0, 2)
if coin == 0:
heads = heads + 1
else:
tails = tails + 1
10 matches
Mail list logo