Discussion:
[Tutor] While Loops: Coin Flip Game
Dawn Samson
2010-11-14 22:16:36 UTC
Permalink
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 coincoin = random.randrange(2)headsCount = 0tailsCount = 0count = 0
# the loopwhile count <= 100: coin if coin == 0: headsCount += 1 if coin == 1: tailsCount += 1 count += 1
print "The number of heads was", headsprint "The number of tails was", tails
raw_input("\n\nPress the enter key to exit.")
Thanks,S. Dawn Samson
Hugo Arts
2010-11-14 22:38:25 UTC
Permalink
Post by Dawn Samson
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
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
Steven D'Aprano
2010-11-14 22:52:07 UTC
Permalink
Post by Dawn Samson
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).
Unfortunately your code has been mangled in the email, but I can guess
your problem: you need to set

coin = random.randrange(2)

each time through the loop, not just once outside the loop.
--
Steven
Stephanie Dawn Samson
2010-11-14 23:05:40 UTC
Permalink
Thanks everyone!
I should be using algorithms for even such programs at my level. The solution to reiterate the coin flip every time in the loop works. Thanks a lot!
Dawn
Adam Bark
2010-11-14 23:09:58 UTC
Permalink
Post by Dawn Samson
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
import random
# set the coin
coin = random.randrange(2)
headsCount = 0
tailsCount = 0
count = 0
# the loop
coin
headsCount += 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
You try to print two variables, "heads" and "tails" which don't exist.
The other replies covered the other main problems.
Robert Berman
2010-11-14 22:47:04 UTC
Permalink
From: tutor-bounces+bermanrl=***@python.org
[mailto:tutor-bounces+bermanrl=***@python.org] On Behalf Of Dawn Samson
Sent: Sunday, November 14, 2010 5:17 PM
To: ***@python.org
Subject: [Tutor] While Loops: Coin Flip Game
Greetings,

I'm a Python beginner and working my way through Michael Dawson's Python
Programming for the Absolute Beginner
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
Hi,

Think in terms of logical steps. You need to generate the results of a random
coin toss for every toss of the coin. Therefore, if you are tossing 100
tosses, you need to generate 100 results based on a probability of zero or
one.

Since this is your homework assignment you write the code, but in terms of a
logical frame work you would have something like the following.
Tossknt = 1
Generate a random toss ; results being either one or 0. Further , assume 0 is
tail; heads is one.
If toss = 1 headcount = headcount + 1
Else tailcount = tailcount + 1
Tossknt = Tossknt + 1
If Tossknt < 100 Continue loop
Else print ‘nbr of heads tossed = ‘, headcount
Print ‘nbr of tails tossed = ‘, tailcount

Obviously, this is not valid python code but it should give you enough
information to solve your problem.

Good luck,

Robert


--
I am using the free version of SPAMfighter.
We are a community of 7 million users fighting spam.
SPAMfighter has removed 53 of my spam emails to date.
Get the free SPAMfighter here: http://www.spamfighter.com/len

The Professional version does not have this message
Thomas C. Hicks
2010-11-15 01:49:41 UTC
Permalink
On Sun, 14 Nov 2010 17:16:36 -0500
Post by Dawn Samson
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
import random
# set the coin
coin = random.randrange(2)
headsCount = 0
tailsCount = 0
count = 0
# the loop
coin
headsCount += 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
Nithya Nisha
2010-11-16 04:08:47 UTC
Permalink
Hi Tom,

Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.
Post by Dawn Samson
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.
Post by Dawn Samson
*coin = random.randrange(2)*
headsCount += 1
else: #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
Post by Dawn Samson
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

_____________________________________________________________________________________________________________________________________________________________
*
Post by Dawn Samson
On Sun, 14 Nov 2010 17:16:36 -0500
Post by Dawn Samson
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
import random
# set the coin
coin = random.randrange(2)
headsCount = 0
tailsCount = 0
count = 0
# the loop
coin
headsCount += 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
Luke Pettit
2010-11-16 04:22:05 UTC
Permalink
When I run this code (I'm also a noob) I get this result:-
Post by Nithya Nisha
Post by Dawn Samson
[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?
Post by Nithya Nisha
Hi Tom,
Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.
Post by Dawn Samson
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.
Post by Dawn Samson
*coin = random.randrange(2)*
headsCount += 1
else: #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
Post by Dawn Samson
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
_____________________________________________________________________________________________________________________________________________________________
*
Post by Dawn Samson
On Sun, 14 Nov 2010 17:16:36 -0500
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
import random
# set the coin
coin = random.randrange(2)
headsCount = 0
tailsCount = 0
count = 0
# the loop
coin
headsCount += 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
Dave Angel
2010-11-16 07:55:09 UTC
Permalink
Post by 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?
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
Nithya Nisha
2010-11-16 08:10:31 UTC
Permalink
Hi there,

This is the Code. Please check it.It is working fine.
Post by Luke Pettit
Post by Dawn Samson
import random
headsCount = 0
tailsCount = 0
count = 1
coin = random.randrange(2)
headsCount += 1
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.")
________________________________________________________________________________________________________________
*
Post by Luke Pettit
When I run this code (I'm also a noob) I get this result:-
[evaluate lines 1-22 from untitled-1.py]
Post by Dawn Samson
Post by Luke Pettit
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
and you wouldn't need to increment count.
DaveA
--
With Regards,
Nithya S
Luke Pettit
2010-11-16 08:21:58 UTC
Permalink
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.
Post by Nithya Nisha
Hi there,
This is the Code. Please check it.It is working fine.
Post by Luke Pettit
Post by Dawn Samson
import random
headsCount = 0
tailsCount = 0
count = 1
coin = random.randrange(2)
headsCount += 1
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.")
________________________________________________________________________________________________________________
*
Post by Luke Pettit
When I run this code (I'm also a noob) I get this result:-
[evaluate lines 1-22 from untitled-1.py]
Post by Dawn Samson
Post by Luke Pettit
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
and you wouldn't need to increment count.
DaveA
--
With Regards,
Nithya S
--
Luke Pettit
Nithya Nisha
2010-11-16 12:41:10 UTC
Permalink
Thankyou..!!!


Regards,
Nithya
Post by 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.
Post by Nithya Nisha
Hi there,
This is the Code. Please check it.It is working fine.
Post by Luke Pettit
Post by Dawn Samson
import random
headsCount = 0
tailsCount = 0
count = 1
coin = random.randrange(2)
headsCount += 1
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.")
________________________________________________________________________________________________________________
*
Post by Luke Pettit
When I run this code (I'm also a noob) I get this result:-
[evaluate lines 1-22 from untitled-1.py]
Post by Dawn Samson
Post by Luke Pettit
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
and you wouldn't need to increment count.
DaveA
--
With Regards,
Nithya S
--
Luke Pettit
Nithya Nisha
2010-11-16 04:41:35 UTC
Permalink
Hi Tom,

Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.
Post by Dawn Samson
import random
# set the coin
headsCount = 0
tailsCount = 0
count = 0
# the loop
#If you declare count = 0. The while loop condition
should be less than 100.Else you will get 101 counts.
Post by Dawn Samson
*coin = random.randrange(2)*
headsCount += 1
else: #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
Post by Dawn Samson
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*
Post by Dawn Samson
On Sun, 14 Nov 2010 17:16:36 -0500
Post by Dawn Samson
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
import random
# set the coin
coin = random.randrange(2)
headsCount = 0
tailsCount = 0
count = 0
# the loop
coin
headsCount += 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
Stephanie Dawn Samson
2010-11-16 20:36:34 UTC
Permalink
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 += 1 else: tailsCount += 1 count += 1

print "The number of heads was", headsCountprint "The number of tails was", tailsCount
raw_input("\n\nPress the enter key to exit.")
Alan Gauld
2010-11-16 22:46:35 UTC
Permalink
Post by Stephanie Dawn Samson
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/
bob gailer
2010-11-17 04:00:22 UTC
Permalink
Just for the heck of it:

heads = sum(random.randrange(2) for i in range(100))
--
Bob Gailer
919-636-4239
Chapel Hill NC
Loading...