Re: [Tutor] Coin flip game

2010-02-12 Thread Albert-Jan Roskam
Hi,

random.choice offers an intuitive way to write the code:

import random
for i in range(10):
    print random.choice(["head", "tail"])

Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Fri, 2/12/10, David  wrote:

From: David 
Subject: Re: [Tutor] Coin flip game
To: tutor@python.org
Date: Friday, February 12, 2010, 1:49 AM

Hello Lawrence,

let me try to clarify this (warning: am a beginner myself).

On 12/02/10 06:15, Jones, Lawrence D wrote:
> Hi,
>
> I'm new to the list and to Python. I'm reading through Michael Dawson's 
> 'Python programming: for absolute beginners' and at the end of chapter 3 he's 
> set a challenge where the reader has to create a coin flip game. My code now 
> works, but only after I randomly switched pieces of the code around and, 
> basically, pulled my hair out because it wouldn't work.
>
> My code is below. But can someone please explain to me why the following 
> variable has to be placed where it is for the code to work? I thought it 
> would need to go nearer the start of the code i.e. just before heads = 0, 
> tails = 0 etc:
>
>                  coin = random.randrange(2)

Python runs through your code, step by step. I believe it starts at the 
top and goes down, following the logic of your code. When you make 
Python refer to a variable in your while loop that Python has not 
encountered yet, then it will not know what to do -- and complain about 
it. Solution: let Python know of the variable _before_ you then start to 
work with it.

>
> Also, why does the randrange integer have to be '2'? I only discovered this 
> worked by complete accident. I tried '1' and '0,1' as my integers but they 
> just didn't work.

That is because your coin has _two_ sides, and you therefore want a 
random choice out of _two_ possibilities. With the random.randrange(2) 
function the choices will be 0 and 1, satisfying your demands. This 
means that the randrange() function goes up to, but not including, the 
integer you supply. It amounts to two choices in the end all the same 
because the counting starts with 0 instead of 1.
That is, if you chose randrange(1) you will get only one answer, namely 
0. If you type randrange(0) then you will get an error message 
(ValueError: empty range for randrange). Which makes sense. Remember, 
randrange() goes up to, but not including the integer supplied.

HTH,

David










>
> Thanks,
>
> Lawrence
>
>
> import random
> print "The Coin Flip Game\n"
>
> heads = 0
> tails = 0
> count = 0
>
> while count<  100:
>      coin = random.randrange(2)
>      if coin == 0:
>          heads = heads + 1
>      else:
>          tails = tails + 1
>      count += 1
>
> print "Heads: ", heads
> print "Tails: ", tails
>
> raw_input("\nPress enter to exit.")
>
>
>
>
>
>
>
> ___
> 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



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


Re: [Tutor] Coin flip game

2010-02-11 Thread Alan Gauld


"Jones, Lawrence D"  wrote 

My code is below. But can someone please explain to me 
why the following variable has to be placed where it is 


Others have explained about variable creation and the fact 
you need to be inside the loop to get different results for 
each iteration.


   coin = random.randrange(2)

I just want to pick up on something you said.
You asked about the "variable".
The variable is "coin". It can go anywhere before the point 
of use, even the first line of your code. You could have done


coin = None 
or 
coin = 0 
and it would work just fine.


What needs to be inside the loop is the call to the 
randrange() function. That is what is simulating the coin flip.
So you need to distinguish the difference between variable 
creation (which in Python happens by means of the first 
assignment of a value)  and function application (where you 
call a function and assign its return value to a variable.) 
In your code you create the variable coin at the same time 
as you apply the function, but you could have done those 
two things separately and the code would still work.


It might seem like I'm splitting hairs but it starts to make 
a difference in some other cases, like this:


while True:
   coin = coin + someFunction()

This will raise an error because you are using the value 
of coin (on the right hand side) before it has been created. 
You need to write it like this:


coin = 0
while True
   coin = coin + someFunction()

It is very important in programming to be clear in your mind 
about these different concepts, especially when deciphering 
error messages.


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] Coin flip game

2010-02-11 Thread David

Hello Lawrence,

let me try to clarify this (warning: am a beginner myself).

On 12/02/10 06:15, Jones, Lawrence D wrote:

Hi,

I'm new to the list and to Python. I'm reading through Michael Dawson's 'Python 
programming: for absolute beginners' and at the end of chapter 3 he's set a 
challenge where the reader has to create a coin flip game. My code now works, 
but only after I randomly switched pieces of the code around and, basically, 
pulled my hair out because it wouldn't work.

My code is below. But can someone please explain to me why the following 
variable has to be placed where it is for the code to work? I thought it would 
need to go nearer the start of the code i.e. just before heads = 0, tails = 0 
etc:

 coin = random.randrange(2)


Python runs through your code, step by step. I believe it starts at the 
top and goes down, following the logic of your code. When you make 
Python refer to a variable in your while loop that Python has not 
encountered yet, then it will not know what to do -- and complain about 
it. Solution: let Python know of the variable _before_ you then start to 
work with it.




Also, why does the randrange integer have to be '2'? I only discovered this 
worked by complete accident. I tried '1' and '0,1' as my integers but they just 
didn't work.


That is because your coin has _two_ sides, and you therefore want a 
random choice out of _two_ possibilities. With the random.randrange(2) 
function the choices will be 0 and 1, satisfying your demands. This 
means that the randrange() function goes up to, but not including, the 
integer you supply. It amounts to two choices in the end all the same 
because the counting starts with 0 instead of 1.
That is, if you chose randrange(1) you will get only one answer, namely 
0. If you type randrange(0) then you will get an error message 
(ValueError: empty range for randrange). Which makes sense. Remember, 
randrange() goes up to, but not including the integer supplied.


HTH,

David












Thanks,

Lawrence


import random
print "The Coin Flip Game\n"

heads = 0
tails = 0
count = 0

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

print "Heads: ", heads
print "Tails: ", tails

raw_input("\nPress enter to exit.")







___
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] Coin flip game

2010-02-11 Thread Benno Lang
On Fri, Feb 12, 2010 at 7:15 AM, Jones, Lawrence D
 wrote:
> My code is below. But can someone please explain to me why the following
> variable has to be placed where it is for the code to work? I thought it
> would need to go nearer the start of the code i.e. just before heads = 0,
> tails = 0 etc:
>     coin = random.randrange(2)

If you put this at the start of the code (before the loop), then you
only flip the coin once, and then count that single flip 100 times.
That would work, but wouldn't be a very useful program.

> Also, why does the randrange integer have to be ‘2’? I only discovered this
> worked by complete accident. I tried ‘1’ and ‘0,1’ as my integers but they
> just didn’t work.

See: http://docs.python.org/library/random.html#random.randrange
random.randrange parameters are the same as for range, which you can
learn more about here:
http://docs.python.org/tutorial/controlflow.html#the-range-function

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


[Tutor] Coin flip game

2010-02-11 Thread Jones, Lawrence D
Hi,

I'm new to the list and to Python. I'm reading through Michael Dawson's 'Python 
programming: for absolute beginners' and at the end of chapter 3 he's set a 
challenge where the reader has to create a coin flip game. My code now works, 
but only after I randomly switched pieces of the code around and, basically, 
pulled my hair out because it wouldn't work.

My code is below. But can someone please explain to me why the following 
variable has to be placed where it is for the code to work? I thought it would 
need to go nearer the start of the code i.e. just before heads = 0, tails = 0 
etc:

coin = random.randrange(2)

Also, why does the randrange integer have to be '2'? I only discovered this 
worked by complete accident. I tried '1' and '0,1' as my integers but they just 
didn't work.

Thanks,

Lawrence


import random
print "The Coin Flip Game\n"

heads = 0
tails = 0
count = 0

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

print "Heads: ", heads
print "Tails: ", tails

raw_input("\nPress enter to exit.")



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


Re: [Tutor] Coin Flip

2008-10-03 Thread bhaaluu
First off, check your program's indentation.

On Fri, Oct 3, 2008 at 2:04 PM, realNewbie <[EMAIL PROTECTED]> wrote:
>
> This is a class assignment, I am to create a program that flips a coin 100
> times and tells me the number of heads and tails.
> I've been working on it for 2 days now and I am stuck. I a trying to run the
> program but it is not running.
> Can someone please point out my error?
>
> Here is what I have come up with:
> import random
> heads=0
> tails=0
> count=1
> while count <101:
>   randomFlip = random.randrange(0,1)
> if randomFlip == 0:
>heads = heads + 1
> else:
>tails = tails + 1
> count = count + 1
> print heads," heads"
> print tails," tails"
> --
> View this message in context: 
> http://www.nabble.com/Coin-Flip-tp19802888p19802888.html
> Sent from the Python - tutor mailing list archive at Nabble.com.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
b h a a l u u at g m a i l dot c o m
Kid on Bus: What are you gonna do today, Napoleon?
Napoleon Dynamite: Whatever I feel like I wanna do. Gosh!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Coin Flip

2008-10-03 Thread taserian
Since the variable *count* never increases inside of the loop body, it gets
stuck in the *while* loop.

I recommend taking a hard look at the program, consider what it should be
doing, and then seeing which statements should be in the *while* loop, and
which ones should be outside it.

Tony R.

On Fri, Oct 3, 2008 at 2:04 PM, realNewbie <[EMAIL PROTECTED]> wrote:

>
> This is a class assignment, I am to create a program that flips a coin 100
> times and tells me the number of heads and tails.
> I've been working on it for 2 days now and I am stuck. I a trying to run
> the
> program but it is not running.
> Can someone please point out my error?
>
> Here is what I have come up with:
> import random
> heads=0
> tails=0
> count=1
> while count <101:
>   randomFlip = random.randrange(0,1)
> if randomFlip == 0:
>heads = heads + 1
> else:
>tails = tails + 1
> count = count + 1
> print heads," heads"
> print tails," tails"
> --
> View this message in context:
> http://www.nabble.com/Coin-Flip-tp19802888p19802888.html
> Sent from the Python - tutor mailing list archive at Nabble.com.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Coin Flip

2008-10-03 Thread realNewbie

This is a class assignment, I am to create a program that flips a coin 100
times and tells me the number of heads and tails.
I've been working on it for 2 days now and I am stuck. I a trying to run the
program but it is not running. 
Can someone please point out my error? 

Here is what I have come up with:
import random
heads=0
tails=0
count=1
while count <101:
   randomFlip = random.randrange(0,1)
if randomFlip == 0:
heads = heads + 1
else:
tails = tails + 1
count = count + 1
print heads," heads"
print tails," tails"
-- 
View this message in context: 
http://www.nabble.com/Coin-Flip-tp19802888p19802888.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor