Newbie: Help Figger Out My Problem
db
duikboot at localhost.localdomain
Tue Jun 28 03:39:14 EDT 2005
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:
> 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
More information about the Python-list
mailing list