Basic coin flipper program - logical error help

D

DannyB

I'm just learning Python. I've created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)


print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I'm sure the [source] tags don't work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Thanks

~Dan
 
J

John McMonagle

I'm just learning Python. I've created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)


print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I'm sure the [source] tags don't work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Yes. Put coin = random.randrange(2) inside the while loop.

import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)

print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
 
C

Claudio Grondi

DannyB said:
I'm just learning Python. I've created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

while (counter < 100):

coin = random.randrange(2)

Claudio
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)


print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I'm sure the [source] tags don't work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Thanks

~Dan
 
L

Larry Bates

DannyB said:
I'm just learning Python. I've created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)


print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I'm sure the [source] tags don't work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Thanks

~Dan
Looks an awful lot like your homework, but I'll give you a clue.
You need to get the your coin tosses inside your loop. Otherwise
you only toss the coin once and then loop 100 times with the
same value.

-Larry Bates
 
W

wes weston

DannyB said:
I'm just learning Python. I've created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)


print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."
[/source]

<<<I'm sure the [source] tags don't work - I through them in there
anyway.>>>

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

Thanks

~Dan

Dan,
Looping is easier with:
for x in range(100):
if random.randint(0,1) == 0:
heads += 1
else:
tails += 1

Inside the loop you need to "flip" on each pass.
You're "flipping" once before the start of the loop now.
wes
 
M

Martin P. Hellwig

DannyB said:
> I'm just learning Python.

So am I :)
> I've created a simple coin flipper program -
> here is the code:
>
> [source]
> #Coin flipper
> import random
>
> heads = 0
> tails = 0
> counter = 0
>
> coin = random.randrange(2)
>
> while (counter < 100):
> if (coin == 0):
> heads += 1
> counter += 1
> else:
> tails += 1
> counter += 1
>
> coin = random.randrange(2)

This line is you logic error because it's not part of your while loop
the coin variables get the result of random.randrange(2) assigned only
one time (before the loop).
>
>
> print "\nThe coin landed on heads", heads, "times."
> print "\nThe coin landed on tails", tails, "times."
> [/source]
>
> <<<I'm sure the [source] tags don't work - I through them in there
> anyway.>>>
>
> The program runs - however - it will give me 100 heads OR 100 tails.
> Can someone spot the logic error?
>
> Thanks
>
> ~Dan
>

You could changed the program to this it works too and is just as
readable (IMHO):

#Coin flipper
import random

heads = 0
tails = 0
counter = 0
# removed random line
while (counter < 100):
if random.randrange(2): # put random here
heads += 1
counter += 1
else:
tails += 1
counter += 1
# removed random line
print "\nThe coin landed on heads", heads, "times."
print "\nThe coin landed on tails", tails, "times."

Take my advice with caution I'm also new to this :)

Btw, it is possible that the coins lands on it side if not catched with
the hand (yes I have seen it happen) ;-)
 
D

DannyB

Thanks everyone for your insight.

I'm coming from C++ - I'm used to formatting code with {} instead of
whitespaces.

@Larry - this isn't my homework :p I'm actually taking a VB.NET class
in school.

I was teaching myself C++ but decided to scale back to Python. I've
heard it was a bit easier to understand and it cuts your development
time by at least 50% (I've heard 90%).

Logically I can figure things out - its the formatting of the logic in
Python that is messing me up. I'll get it soon enough =)
 
J

John Zenger

wes said:
Looping is easier with:
for x in range(100):
if random.randint(0,1) == 0:
heads += 1
else:
tails += 1

Also, with the functional programming tools of map, filter, and lambda,
this code can be reduced to just six lines:

import random

flips = map(lambda x: random.randrange(2), xrange(100))
heads = len(filter(lambda x: x is 0, flips))
tails = len(filter(lambda x: x is not 0, flips))

print "The coin landed on heads", heads, "times."
print "The coin landed on tails", tails, "times."
 
B

bonono

John said:
Also, with the functional programming tools of map, filter, and lambda,
this code can be reduced to just six lines:

import random

flips = map(lambda x: random.randrange(2), xrange(100))
heads = len(filter(lambda x: x is 0, flips))
tails = len(filter(lambda x: x is not 0, flips))

Or a filter/map/lambda free way:

heads = sum(random.randrange(2) for x in xrange(100))
tails = 100 - heads
 
P

Paul McGuire

Or a filter/map/lambda free way:

heads = sum(random.randrange(2) for x in xrange(100))
tails = 100 - heads
sort, then groupby.


import itertools
import random
h,t = [len(list(g)) for k,g in itertools.groupby(sorted([random.randrange(2)
for i in xrange(100)]))]
print h,t
 
D

Dennis Lee Bieber

I'm just learning Python. I've created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0
Presuming a coin can only be heads OR tails (never both, and never
neither) you don't need three accumulators:

counter == tails + heads
coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)
Many have mentioned that you generate one random coin, count that
one coin 100 times, then generate a second random coin that you throw
away.

If all you need is the count of the heads or tails, but never need
to do anything with any single coin... Let's see...


import random
RUN = 100
heads = len([x for x in xrange(RUN) if random.randrange(2)])
print "There were %s heads and %s tails in %s flips" % (heads, RUN-heads, RUN) There were 51 heads and 49 tails in 100 flips

RUN = 1000
heads = len([x for x in xrange(RUN) if random.randrange(2)])
print "There were %s heads and %s tails in %s flips" % (heads, RUN-heads, RUN) There were 503 heads and 497 tails in 1000 flips

--
 
J

Jeffrey Schwab

wes said:
....

Dan,
Looping is easier with:
for x in range(100):
if random.randint(0,1) == 0:
heads += 1
else:
tails += 1

Or, continuing with that theme:

for x in range(N):
heads += random.randint(0, 1)

As in:

import random
N = 100
heads = 0
for x in range(N):
heads += random.randint(0, 1)
print "%d heads and %d tails." % (heads, N - heads)
 
B

Brian van den Broek

DannyB said unto the world upon 21/02/06 06:14 PM:
I'm just learning Python. I've created a simple coin flipper program -
here is the code:

[source]
#Coin flipper
import random

heads = 0
tails = 0
counter = 0

coin = random.randrange(2)

while (counter < 100):
if (coin == 0):
heads += 1
counter += 1
else:
tails += 1
counter += 1

coin = random.randrange(2)

The program runs - however - it will give me 100 heads OR 100 tails.
Can someone spot the logic error?

<snip>

Your original question is long since answered. But I've a style point.
As Dennis Lee Bieber pointed out, you don't need all three
accumulators. If you keep to the overall style of your code, you can
avoid repeating yourself as:


while (counter < 100):
counter += 1 # No point in putting this in each branch
coin = random.randrange(2)
if (coin == 0):
heads += 1
else:
tails += 1


For roughly the same style, I'd go with:

heads = 0
count = 100

for i in range(count):
if random.randrange(2):
heads += 1

tails = count - heads

HTH,

Brian vdB
 
P

Paul McGuire

Paul McGuire said:
sort, then groupby.


import itertools
import random
h,t = [len(list(g)) for k,g in itertools.groupby(sorted([random.randrange(2)
for i in xrange(100)]))]
print h,t
By the way, sort + groupby generalizes beyond just coin-flipping. Here is a
modified version that simulates die rolls.

-- Paul

import itertools
import random

NUM_ROLLS = 1000
dieRolls = [random.randrange(6)+random.randrange(6)+2 for i in
xrange(NUM_ROLLS)]

# create dummy list entries for impossible rolls of 0 and 1
rolls = [None,None]

rolls += [len(list(g)) for k,g in itertools.groupby(sorted(dieRolls))]

# print out nice histogram
for i,r in enumerate(rolls):
if i > 1:
print "%2d - %s" % (i,"*"*int(round(r/10.0)))


prints:

2 - ***
3 - *****
4 - *********
5 - **********
6 - ***************
7 - ******************
8 - **************
9 - *********
10 - *******
11 - *******
12 - ***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top