Quantcast

Coin flip game

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Coin flip game

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  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Coin flip game

Benno Lang-2
On Fri, Feb 12, 2010 at 7:15 AM, Jones, Lawrence D
<[hidden email]> 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  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Coin flip game

David-591
In reply to this post by Jones, Lawrence D
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  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Coin flip game

Alan Gauld
In reply to this post by Jones, Lawrence D

"Jones, Lawrence D" <[hidden email]> 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  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Coin flip game

Albert-jan Roskam
In reply to this post by David-591
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 <[hidden email]> wrote:

From: David <[hidden email]>
Subject: Re: [Tutor] Coin flip game
To: [hidden email]
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@...
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Loading...