Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-16 Thread bob gailer

Just for the heck of it:

heads = sum(random.randrange(2) for i in range(100))

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-16 Thread Alan Gauld


"Stephanie Dawn Samson"  wrote

 thought I should write the rewritten code I got that others helped 
me get to


By applying a couple of other ideas that have been suggested you get
something shorter and arguably slightly clearer:

# Coin Flips
# The program flips a coin 100 times and then
# tells you the number of heads and tails

import random

print """
   Welcome to 'Coin Flipper!'
I will flip a coin 100 times and then tell you
the number of heads and tails!
"""
headsCount = 0

for count in range(100):
   if random.randrange(2):  # returns 1/0 => true/False
  headsCount += 1

print "The number of heads was", headsCount
print "The number of tails was", 100-headsCount
raw_input("\n\nPress the enter key to exit.")


And you could replace the whole for loop with a generator expression
if you really wanted to, but I suspect that is getting into advanced
territory for you at this stage...

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-16 Thread Stephanie Dawn Samson

Greetings,
As a thread starter, I thought I should write the rewritten code I got that 
others helped me get to, since this thread is still going on.

# Coin Flips# The program flips a coin 100 times and then# tells you the number 
of heads and tailsimport random
print "\a"print "\tWelcome to 'Coin Flipper!'"print "\nI will flip a coin 100 
times and then tell you"print "the number of heads and tails!\n"
# set the coinheadsCount = 0tailsCount = 0count = 1
while count <= 100:coin = random.randrange(2)if coin == 0:
headsCount += 1else:tailsCount += 1count += 1

print "The number of heads was", headsCountprint "The number of tails was", 
tailsCount
raw_input("\n\nPress the enter key to exit.")
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-16 Thread Nithya Nisha
Thankyou..!!!


Regards,
Nithya

On Tue, Nov 16, 2010 at 1:51 PM, Luke Pettit  wrote:

> Arrr thats better Nithya it works fine now. I had it working fine before
> it was just coming up with that strange result of 73 and 100
> when I copied the code into wing to check it out in order to understand it.
> Wing picked up the spacing and I had already corrected
> that Dave as I was simply looking at Nithya code.
>
>
> On 16 November 2010 19:10, Nithya Nisha  wrote:
>
>> Hi there,
>>
>> This is the Code. Please check it.It is working fine.
>>
>> >>>import random
>> >>>headsCount = 0
>> >>>tailsCount = 0
>> >>>count = 1
>> >>>
>> >>>while count <= 100:
>> >>>   coin = random.randrange(2)
>> >>>   if coin == 0:
>> >>> headsCount += 1
>> >>>   else:
>> >>> tailsCount += 1
>> >>>   count += 1
>> >>>
>> >>>print "The number of heads was", headsCount
>> >>>print "The number of tails was", tailsCount
>> >>>
>> >>>raw_input("\n\nPress the enter key to exit.")
>>
>>
>> 
>> *
>> Your Description *:
>>
>> On Tue, Nov 16, 2010 at 1:25 PM, Dave Angel  wrote:
>>
>>>  When I run this code (I'm also a noob) I get this result:-

  [evaluate lines 1-22 from untitled-1.py]
>>>
>> The number of heads was 73
 The number of tails was 100

 Press the enter key to exit.

 # Surely, if flipping a single coin 100 times your total number of heads
 and
 tails should add up to 100
 # not 173 or am I missing the point?


  No, you're missing an indentation.  If you check the code you're
>>> running, I think you'll find that you didn't unindent the line incrementing
>>> count.
>>>
>>> Of course, it's less error prone to simply use
>>> for count in xrange(100):
>>>
>>> instead of while count < 100:
>>>
>>> and you wouldn't need to increment count.
>>>
>>> DaveA
>>>
>>>
>>
>>
>> --
>> With Regards,
>> Nithya S
>>
>>
>
>
> --
> Luke Pettit
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-16 Thread Luke Pettit
Arrr thats better Nithya it works fine now. I had it working fine before it
was just coming up with that strange result of 73 and 100
when I copied the code into wing to check it out in order to understand it.
Wing picked up the spacing and I had already corrected
that Dave as I was simply looking at Nithya code.

On 16 November 2010 19:10, Nithya Nisha  wrote:

> Hi there,
>
> This is the Code. Please check it.It is working fine.
>
> >>>import random
> >>>headsCount = 0
> >>>tailsCount = 0
> >>>count = 1
> >>>
> >>>while count <= 100:
> >>>   coin = random.randrange(2)
> >>>   if coin == 0:
> >>> headsCount += 1
> >>>   else:
> >>> tailsCount += 1
> >>>   count += 1
> >>>
> >>>print "The number of heads was", headsCount
> >>>print "The number of tails was", tailsCount
> >>>
> >>>raw_input("\n\nPress the enter key to exit.")
>
>
> 
> *
> Your Description *:
>
> On Tue, Nov 16, 2010 at 1:25 PM, Dave Angel  wrote:
>
>>  When I run this code (I'm also a noob) I get this result:-
>>>
>>>  [evaluate lines 1-22 from untitled-1.py]
>>
> The number of heads was 73
>>> The number of tails was 100
>>>
>>> Press the enter key to exit.
>>>
>>> # Surely, if flipping a single coin 100 times your total number of heads
>>> and
>>> tails should add up to 100
>>> # not 173 or am I missing the point?
>>>
>>>
>>>  No, you're missing an indentation.  If you check the code you're
>> running, I think you'll find that you didn't unindent the line incrementing
>> count.
>>
>> Of course, it's less error prone to simply use
>> for count in xrange(100):
>>
>> instead of while count < 100:
>>
>> and you wouldn't need to increment count.
>>
>> DaveA
>>
>>
>
>
> --
> With Regards,
> Nithya S
>
>


-- 
Luke Pettit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-16 Thread Nithya Nisha
Hi there,

This is the Code. Please check it.It is working fine.

>>>import random
>>>headsCount = 0
>>>tailsCount = 0
>>>count = 1
>>>
>>>while count <= 100:
>>>   coin = random.randrange(2)
>>>   if coin == 0:
>>> headsCount += 1
>>>   else:
>>> tailsCount += 1
>>>   count += 1
>>>
>>>print "The number of heads was", headsCount
>>>print "The number of tails was", tailsCount
>>>
>>>raw_input("\n\nPress the enter key to exit.")


*
Your Description *:

On Tue, Nov 16, 2010 at 1:25 PM, Dave Angel  wrote:

> When I run this code (I'm also a noob) I get this result:-
>>
>>  [evaluate lines 1-22 from untitled-1.py]
>
 The number of heads was 73
>> The number of tails was 100
>>
>> Press the enter key to exit.
>>
>> # Surely, if flipping a single coin 100 times your total number of heads
>> and
>> tails should add up to 100
>> # not 173 or am I missing the point?
>>
>>
>>  No, you're missing an indentation.  If you check the code you're running,
> I think you'll find that you didn't unindent the line incrementing count.
>
> Of course, it's less error prone to simply use
> for count in xrange(100):
>
> instead of while count < 100:
>
> and you wouldn't need to increment count.
>
> DaveA
>
>


-- 
With Regards,
Nithya S
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Dave Angel

When I run this code (I'm also a noob) I get this result:-


[evaluate lines 1-22 from untitled-1.py]

The number of heads was 73
The number of tails was 100

Press the enter key to exit.

# Surely, if flipping a single coin 100 times your total number of heads and
tails should add up to 100
# not 173 or am I missing the point?


No, you're missing an indentation.  If you check the code you're 
running, I think you'll find that you didn't unindent the line 
incrementing count.


Of course, it's less error prone to simply use
for count in xrange(100):

instead of while count < 100:

and you wouldn't need to increment count.

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Nithya Nisha
Hi Tom,

Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.


> import random
>
> # set the coin

> headsCount = 0
> tailsCount = 0
> count = 0
>
> # the loop
> while count < 100:
 #If you declare count = 0. The while loop condition
should be  less than 100.Else you will get 101 counts.
> *coin = random.randrange(2)*

> if coin == 0:
> headsCount += 1
>else:   #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
> tailsCount += 1
> count += 1
>
>
> print "The number of heads was", headsCount
> print "The number of tails was", tailsCount

>
> raw_input("\n\nPress the enter key to exit.")

Regards,
Nithya

__
*Your Description*

On Mon, Nov 15, 2010 at 7:19 AM, Thomas C. Hicks  wrote:

> On Sun, 14 Nov 2010 17:16:36 -0500
> Dawn Samson  wrote:
>
> > Greetings,
> >
> > I'm a Python beginner and working my way through Michael Dawson's
> > Python Programming for the Absolute Beginner. I'm stuck in a
> > particular challenge that asks me to write a program that "flips a
> > coin 100 times and then tells you the number of heads and tails."
> > I've been trying to work on this challenge for a while now and can't
> > get it to work (either it has 100 heads or 100 tails). I've been
> > reworking this code many times and currently what I have does not do
> > anything at all at IDLE. Please take a look at my code below:
> >
> > import random
> >
> > # set the coin
> > coin = random.randrange(2)
> > headsCount = 0
> > tailsCount = 0
> > count = 0
> >
> > # the loop
> > while count <= 100:
> > coin
> > if coin == 0:
> > headsCount += 1
> > if coin == 1:
> > tailsCount += 1
> > count += 1
> >
> >
> > print "The number of heads was", heads
> > print "The number of tails was", tails
> >
> > raw_input("\n\nPress the enter key to exit.")
> >
> > Thanks,
> > S. Dawn Samson
>
> >From one beginner to another - it looks to me like you set the value of
> coin once then checked it 100 times.  If you want to reset the value of
> coin maybe it (i.e. setting the value of coin, not just calling
> the value of coin) should be in the loop too?
>
> tom
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
With Regards,
Nithya S
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Luke Pettit
When I run this code (I'm also a noob) I get this result:-

>>> [evaluate lines 1-22 from untitled-1.py]
The number of heads was 73
The number of tails was 100

Press the enter key to exit.

# Surely, if flipping a single coin 100 times your total number of heads and
tails should add up to 100
# not 173 or am I missing the point?



On 16 November 2010 15:08, Nithya Nisha  wrote:

> Hi Tom,
>
> Your code is almost correct. Little mistake is there.The random number
> generate should be in while loop.
>
> > import random
> >
> > # set the coin
>
> > headsCount = 0
> > tailsCount = 0
> > count = 0
> >
> > # the loop
> > while count < 100:   #If
> you declare count = 0. The while loop condition should be  less than
> 100.Else you will get 101 counts.
> > *coin = random.randrange(2)*
> > if coin == 0:
> > headsCount += 1
> >else:   #Becase We
> already declared randrange(2).So the coin value is 0 or 1.So we can use else
> condition.
> > tailsCount += 1
> > count += 1
> >
> >
> > print "The number of heads was", headsCount
> > print "The number of tails was", tailsCount
> >
> > raw_input("\n\nPress the enter key to exit.")
>
> Regards,
> Nithya
>
>
> _
> *
> Your Description*:
>
> On Mon, Nov 15, 2010 at 7:19 AM, Thomas C. Hicks wrote:
>
>> On Sun, 14 Nov 2010 17:16:36 -0500
>> Dawn Samson  wrote:
>>
>> > Greetings,
>> >
>> > I'm a Python beginner and working my way through Michael Dawson's
>> > Python Programming for the Absolute Beginner. I'm stuck in a
>> > particular challenge that asks me to write a program that "flips a
>> > coin 100 times and then tells you the number of heads and tails."
>> > I've been trying to work on this challenge for a while now and can't
>> > get it to work (either it has 100 heads or 100 tails). I've been
>> > reworking this code many times and currently what I have does not do
>> > anything at all at IDLE. Please take a look at my code below:
>> >
>> > import random
>> >
>> > # set the coin
>> > coin = random.randrange(2)
>> > headsCount = 0
>> > tailsCount = 0
>> > count = 0
>> >
>> > # the loop
>> > while count <= 100:
>> > coin
>> > if coin == 0:
>> > headsCount += 1
>> > if coin == 1:
>> > tailsCount += 1
>> > count += 1
>> >
>> >
>> > print "The number of heads was", heads
>> > print "The number of tails was", tails
>> >
>> > raw_input("\n\nPress the enter key to exit.")
>> >
>> > Thanks,
>> > S. Dawn Samson
>>
>> >From one beginner to another - it looks to me like you set the value of
>> coin once then checked it 100 times.  If you want to reset the value of
>> coin maybe it (i.e. setting the value of coin, not just calling
>> the value of coin) should be in the loop too?
>>
>> tom
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Luke Pettit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Nithya Nisha
Hi Tom,

Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.

> import random
>
> # set the coin

> headsCount = 0
> tailsCount = 0
> count = 0
>
> # the loop
> while count < 100:   #If
you declare count = 0. The while loop condition should be  less than
100.Else you will get 101 counts.
> *coin = random.randrange(2)*
> if coin == 0:
> headsCount += 1
>else:   #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
> tailsCount += 1
> count += 1
>
>
> print "The number of heads was", headsCount
> print "The number of tails was", tailsCount
>
> raw_input("\n\nPress the enter key to exit.")

Regards,
Nithya

_
*
Your Description*:

On Mon, Nov 15, 2010 at 7:19 AM, Thomas C. Hicks  wrote:

> On Sun, 14 Nov 2010 17:16:36 -0500
> Dawn Samson  wrote:
>
> > Greetings,
> >
> > I'm a Python beginner and working my way through Michael Dawson's
> > Python Programming for the Absolute Beginner. I'm stuck in a
> > particular challenge that asks me to write a program that "flips a
> > coin 100 times and then tells you the number of heads and tails."
> > I've been trying to work on this challenge for a while now and can't
> > get it to work (either it has 100 heads or 100 tails). I've been
> > reworking this code many times and currently what I have does not do
> > anything at all at IDLE. Please take a look at my code below:
> >
> > import random
> >
> > # set the coin
> > coin = random.randrange(2)
> > headsCount = 0
> > tailsCount = 0
> > count = 0
> >
> > # the loop
> > while count <= 100:
> > coin
> > if coin == 0:
> > headsCount += 1
> > if coin == 1:
> > tailsCount += 1
> > count += 1
> >
> >
> > print "The number of heads was", heads
> > print "The number of tails was", tails
> >
> > raw_input("\n\nPress the enter key to exit.")
> >
> > Thanks,
> > S. Dawn Samson
>
> >From one beginner to another - it looks to me like you set the value of
> coin once then checked it 100 times.  If you want to reset the value of
> coin maybe it (i.e. setting the value of coin, not just calling
> the value of coin) should be in the loop too?
>
> tom
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-14 Thread Thomas C. Hicks
On Sun, 14 Nov 2010 17:16:36 -0500
Dawn Samson  wrote:

> Greetings,
> 
> I'm a Python beginner and working my way through Michael Dawson's
> Python Programming for the Absolute Beginner. I'm stuck in a
> particular challenge that asks me to write a program that "flips a
> coin 100 times and then tells you the number of heads and tails."
> I've been trying to work on this challenge for a while now and can't
> get it to work (either it has 100 heads or 100 tails). I've been
> reworking this code many times and currently what I have does not do
> anything at all at IDLE. Please take a look at my code below:
> 
> import random
> 
> # set the coin
> coin = random.randrange(2)
> headsCount = 0
> tailsCount = 0
> count = 0
> 
> # the loop
> while count <= 100:
> coin
> if coin == 0:
> headsCount += 1
> if coin == 1:
> tailsCount += 1
> count += 1
> 
> 
> print "The number of heads was", heads
> print "The number of tails was", tails
> 
> raw_input("\n\nPress the enter key to exit.")
> 
> Thanks,
> S. Dawn Samson

>From one beginner to another - it looks to me like you set the value of
coin once then checked it 100 times.  If you want to reset the value of
coin maybe it (i.e. setting the value of coin, not just calling
the value of coin) should be in the loop too?

tom
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor