beginner code problem

R

RJ

I'm trying to teach myself Python (probably running into the old dog
new tricks issue) and I'm trying to start with the very basics to get a
handle on them.

I'm trying to write code to get the computer to flip a coin 100 times
and give me the output of how many times heads and tails. After solving
a few syntax errors I seem to be stuck in an endless loop and have to
kill python. A few times I would get it to print 'heads 0 (or 1) times
and tails 1 (or 0) times' 100 times.

Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:

if flip == 0:
heads += 1

else:
tails += 1


count += 1



print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'
 
J

John Machin

I'm trying to teach myself Python (probably running into the old dog
new tricks issue) and I'm trying to start with the very basics to get a
handle on them.

I'm trying to write code to get the computer to flip a coin 100 times
and give me the output of how many times heads and tails. After solving
a few syntax errors I seem to be stuck in an endless loop and have to
kill python.

This can't happen with the code that you posted. It *could* happen if
the statement count += 1 was not being executed once each time around
the while loop -- like if it was *not* indented.

# Bad code #1
import random
flip = random.randrange(2)
heads = tails = count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'
A few times I would get it to print 'heads 0 (or 1) times
and tails 1 (or 0) times' 100 times.

Again, can't happen with the code you have posted. If it is printing 100
times, that would be because you have indented the print statement so
that it is being executed once each trip around the loop.

# Bad code #2
import random
flip = random.randrange(2)
heads = tails = count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'
Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:

To help you see what is happening, insert a print statement here; e.g.:
print flip, count, heads, tails
if flip == 0:
heads += 1

else:
tails += 1


count += 1



print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

The code that you posted sets flip only once i.e. only 1 toss, not 100.
If it is 0, you get 100 heads and 0 tails. Otherwise you get 0 heads and
100 tails. You need to get a new value for flip each trip.

# Not quite so bad code
import random
heads = tails = count = 0
while count < 100:
flip = random.randrange(2)
# print flip, count, heads, tails # un-comment as/when required :)
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

HTH,
John
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Hello
Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:

if flip == 0:

flip never changes again
it's not reassigned in the while loop
heads += 1

else:
tails += 1


count += 1



print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'


in case you know how many times to iterate
it's better to use for loop (in python and eq C also)

from random import randrange as flip
result = [0,0]
for i in range(100):
result[flip(2)] += 1

or

from random import randrange as flip
result = {"head":0,
"tail":0}
for i in range(100):
result[["head","tail"]flip(2)] += 1

or
.... def flip(self):
.... import random
.... return ("head", "tail")[random.randrange(2)]
c = Coin()
result = {"head":0,"tail":0}
for i in range(100):
result[c.flip()] += 1

or many many more
the important thing is .. to know what is the
most suitable data representation for you

is it throw-away-code or is this going to be read
by other people .. etc

hth, Daniel
 
D

DaveM

I'm trying to teach myself Python (probably running into the old dog
new tricks issue) and I'm trying to start with the very basics to get a
handle on them.

I'm trying to write code to get the computer to flip a coin 100 times
and give me the output of how many times heads and tails. After solving
a few syntax errors I seem to be stuck in an endless loop and have to
kill python. A few times I would get it to print 'heads 0 (or 1) times
and tails 1 (or 0) times' 100 times.

Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:

if flip == 0:
heads += 1

else:
tails += 1


count += 1



print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

Several problems here. "flip" is defined just once, so you'll either have
100 heads or tails and your whitespace is all wrong - that's important in
Python. Here's how it should look:

import random

def coinflip():
heads = 0
tails = 0
for goes in range(100):
if random.randrange(2) == 1:
heads += 1
else:
tails += 1
print "heads", heads
print "tails", tails

if __name__ == "__main__":
coinflip()
 
R

Rob Johnson

Thanks for the reply John. I seem to be getting all the same problems
with your code that I had with mine so it may be an issue with Python
on this computer though I haven't had one prior to now. I'm running it
on OSX Tiger so I'll give it a shot on my Windows box. With pythons
portability I didn't think it would be an issue so didn't mention it in
the post.

The different problems I was having was a result of moving some of
the code around thinking I had messed up and trying to fix it. The code
I posted was just running but never stopping to give the the output.

Thanks for explaining about getting a new value for flip, I wasn't
positive about that and you really helped clear up any confusion I was
having.

Rob
 
R

Rene Pijlman

RJ:
import random
flip = random.randrange(2)
heads = 0
tails = 0
count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1

Since flip isn't changed in the loop, this is going to report 100 heads or
100 tails, depending on the zeroness of flip.
 
R

RJ

Thanks for all the help and examples. As I mentioned, I'm trying to
teach myself and starting with the basics (if, elif, else, while, for,
raw_input, int are about all I know so far and don't know that well
yet) Some of the example posted are still beyond my understanding but
it's good to see other ways of acheiving the result and other things
to look up and read about.

I've read a bit about Python but only really started trying
yesterday. Right now I'm still strugling but with practice I hope for
it to come easier and I'll keep learning more syntax.

I didn't even think about random.randrange being out of the loop but
it makes sence now. Chalk it up to a newbie mistake and a learning
experience.

I really do appreciate the help and I'm sure this is only the first
of many questions I'll have. It's good to know that there are others
who can help me understand my mistakes.

Rob
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top