[Tutor] While Loops: Coin Flip Game

Hugo Arts hugo.yoshi at gmail.com
Sun Nov 14 23:38:25 CET 2010


On Sun, Nov 14, 2010 at 11:16 PM, Dawn Samson <sdawn at live.ca> 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:

When you, as you say it, "set the coin," you call the randrange
function. This picks a random number between 0 and 1 and returns it.
You then assign that number to the coin variable. So at this point,
coin contains either 0 or 1.

In the while loop, you have a line that simply says "coin." You
probably think that this calls the function again, but it doesn't.
What it actually does is nothing. You simply mention the variable to
the interpreter, which retrieves its value (0 or 1). Since you don't
do anything with that value, it is simply immediately discarded again.

You need to change your code so that the coin isn't set only once, but
every iteration of the loop. Hope that helps

Hugo


More information about the Tutor mailing list