Newbie: Help Figger Out My Problem

C

ChuckDubya

##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
flips = flips + 1
if flips >= 99:
print "Heads: " + heads
print "Tails: " + tails
print "Total: " + flips + "flips"
raw_input("Press the enter key to exit.")



When I save and run this "program", I get a DOS window that flashes at
me and disappears. What's wrong with it?
 
P

Paul Rubin

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 and run the program from the command line. Just
type "myprog.py" to the C:> prompt (or whatever your .py file is
called) and it will run in the dos box. Of course you have to be
in the directory where the .py file is.

2) run IDLE, the integrated Python environment that you probably have
installd. Select it from the programs menu. Go to fhe file pulldown
and open your .py file (you may have to browse around for it). Then
hit "F5" and IDLE will pop another window that shows you the output.
 
D

db

##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
flips = flips + 1
if flips >= 99:
print "Heads: " + heads
print "Tails: " + tails
print "Total: " + flips + "flips"
raw_input("Press the enter key to exit.")



When I save and run this "program", I get a DOS window that flashes at
me and disappears. What's wrong with it?
Your programm gives an error. You are trying to concatenate strings with integers.

http://docs.python.org/lib/typesseq-strings.html

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
flips = flips + 1
if flips >= 99:
print "Heads: %s" % heads ## string formatting
print "Tails: %s" %tails ## string formatting
print "Total: %s flips" % flips ## string formatting
raw_input("Press the enter key to exit.")

hth Arjen
 
Q

qwweeeit

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

# --------------------------------------
# profiling: 15.3 seconds
heads = 0
tails = 0
flips = 0
while flips < 999999:
coin = random.randrange(0, 2)
if coin == 0:
heads = heads + 1
else:
tails = tails + 1
flips = flips + 1
print "heads",heads
print "tails",tails
print "flips",flips
nSecondsTM=time.time()-nStart
print "seconds taken by the traditional method",nSecondsTM
#---------------------------------------

# profiling: 16.8 seconds
nFlips=0
lFlip=[] # list initialization
while nFlips < 999999:
lFlip.append(random.randrange(0, 2)) # filling the list
nFlips += 1

print "heads",lFlip.count(0)
print "tails",lFlip.count(1)
print "flips",nFlips
print "seconds taken by the list method",time.time()-nStart-nSecondsTM
# --------------------------------------

Perhaps there are other more clever approaches...
Bye.
 
B

bruno modulix

##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 heads += 1
else:
tails = tails + 1 idem
flips = flips + 1 idem
if flips >= 99:
Why do you think you need this test ?-)
print "Heads: " + heads print "Heads: %d" % heads
print "Tails: " + tails idem
print "Total: " + flips + "flips" idem
raw_input("Press the enter key to exit.")

May I suggest this version ?


from random import randrange

def flip_coins(flips):
coins = [0, 0]
for i in xrange(flips):
coins[randrange(0, 2)] += 1
return coins[0], coins[1]

if __name__ == "__main__":
flips = 100
heads, tails = flip_coins(flips)
print "Heads: %d\nTails %d\nTotal: %d flips\n" % (heads, tails, flips)
#raw_input("Press the enter key to exit.")


HTH
 
S

Scott David Daniels

db said:
Your programm gives an error. You are trying to concatenate strings with integers.
Might I suggest you were looking for this:
...
print "Heads: ", heads
print "Tails: ", tails
print "Total: ", flips, "flips"
...

--Scott David Daniels
(e-mail address removed)
 
D

Devan L

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

Casey Hawthorne

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 = 1000000
for i in xrange(flips):
if random.randint(0,1): heads += 1
print "Heads:%s" % heads
print "Tails:%s" % (flips - heads)


Devan L said:
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.
 
C

ChuckDubya

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

bruno modulix

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

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top