473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Basic coin flipper program - logical error help

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

Feb 22 '06 #1
12 2829
On Tue, 2006-02-21 at 16:14 -0800, DannyB wrote:
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."
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Feb 22 '06 #2
DannyB wrote:
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

Feb 22 '06 #3
DannyB wrote:
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
Feb 22 '06 #4
DannyB wrote:
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
Feb 22 '06 #5
DannyB wrote:
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) ;-)

--
mph
Feb 22 '06 #6
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 =)

Feb 22 '06 #7
wes weston wrote:
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."
Feb 22 '06 #8

John Zenger wrote:
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

Feb 22 '06 #9
<bo****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

John Zenger wrote:
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

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

Feb 22 '06 #10
wes weston wrote:
DannyB wrote:
I'm just learning Python. I've created a simple coin flipper program -

....
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)
Feb 22 '06 #11
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)
<snip>

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
Feb 22 '06 #12

"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:yU******************@tornado.texas.rr.com...
<bo****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... 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 - ***

Feb 22 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Monteleone | last post by:
Greetings, I posted a few days back and didnt get much of a response, so I figured I would post again with more detail. I am running gnome under fedora core 4. I want a kid to be able to drop a...
3
by: viewsonic | last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t should have the following parameters. output is: Name Date total...
4
by: junaidnaseer | last post by:
Hi ! I am facing a problem that I have defined a function which when called in the same file generates an error as follows; " visual c error C2371 redefinition basic types see...
52
by: celerysoup16 | last post by:
I've written this coin toss program, and can't figure out why it isn't giving accurate results... cheers, Ben #include <stdlib.h> #include <stdio.h> #define H 1 #define T 0 #define SENTINEL...
7
by: Joey | last post by:
I don't care what .net language this come in but I really need to determine if a disk is dynamic or basic. I have posted something in the WMI group but no one knows how to do it. Does anyone have...
5
by: sallyk57 | last post by:
I have to make a program that would ask a user for their guess (heads or tails) and tells them if its correct or not then it has to ask the user if they want to play again. I figured out how to do...
6
blackstormdragon
by: blackstormdragon | last post by:
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine: #include<iostream> #include<cstdlib> using namespace std; int flip(); void main...
1
by: chrspta | last post by:
I am new to Visual basic. I need a program using VB6 that converts txt files to excel file.Description is in the below: The form should have the Drive list, Dir list, file list and cmdConvert...
8
by: PAK11 | last post by:
This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.