beginner code problem

2006-06-02 Thread 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'

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner code problem

2006-06-02 Thread John Machin
On 3/06/2006 8:41 AM, RJ wrote:
  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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner code problem

2006-06-02 Thread Schüle 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

  class Coin:
... 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner code problem

2006-06-02 Thread DaveM
On Fri, 2 Jun 2006 18:41:53 -0400, RJ [EMAIL PROTECTED] wrote:

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner code problem

2006-06-02 Thread Rob Johnson
On 2006-06-02 19:25:28 -0400, John Machin [EMAIL PROTECTED] said:

 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

 
 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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner code problem

2006-06-02 Thread 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.

-- 
René Pijlman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner code problem

2006-06-02 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list