[Tutor] Coin flip game
Albert-Jan Roskam
fomcl at yahoo.com
Fri Feb 12 12:12:52 CET 2010
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 <ldl08 at gmx.net> wrote:
From: David <ldl08 at gmx.net>
Subject: Re: [Tutor] Coin flip game
To: tutor at 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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100212/d93e047d/attachment.htm>
More information about the Tutor
mailing list