473,803 Members | 3,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.randrang e(2)

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

coin = random.randrang e(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 2857
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.randrang e(2)

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

coin = random.randrang e(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.randrang e(2) inside the while loop.

import random

heads = 0
tails = 0
counter = 0

coin = random.randrang e(2)

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

coin = random.randrang e(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.randrang e(2)

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

coin = random.randrang e(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.randrang e(2)

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

coin = random.randrang e(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.randrang e(2)

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

coin = random.randrang e(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.randrang e(2)

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

coin = random.randrang e(2)
This line is you logic error because it's not part of your while loop
the coin variables get the result of random.randrang e(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.randrang e(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.randrang e(2), xrange(100))
heads = len(filter(lamb da x: x is 0, flips))
tails = len(filter(lamb da 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.randrang e(2), xrange(100))
heads = len(filter(lamb da x: x is 0, flips))
tails = len(filter(lamb da x: x is not 0, flips))


Or a filter/map/lambda free way:

heads = sum(random.rand range(2) for x in xrange(100))
tails = 100 - heads

Feb 22 '06 #9
<bo****@gmail.c om> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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.randrang e(2), xrange(100))
heads = len(filter(lamb da x: x is 0, flips))
tails = len(filter(lamb da x: x is not 0, flips))


Or a filter/map/lambda free way:

heads = sum(random.rand range(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.group by(sorted([random.randrang e(2)
for i in xrange(100)]))]
print h,t

Feb 22 '06 #10

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

Similar topics

0
1680
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 quarter into a coin slot and get 15 minutes of time on an account that has "restricted" Internet access via the firefox web browser. We are experimenting with different ways to allow kids to safely surf the web during school hours while...
3
16943
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 number of coins number of quarters number of dimes
4
10350
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 declaration of the function . " Could anyone please help me with this . Thank you
52
5411
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 -1
7
10766
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 any idea how I can determine this? At this point I don't care if it is a third plugin freeware app I use that writes it to a text file that I could read out. Thanks!
5
3847
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 this but the part that i am stuck on is how to print a summary at the end of this program tracking how many wins and how many losses. import cs1.Keyboard; public class CoinToss6 { public static void main(String args) { ...
6
6078
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 () { int coin, counter, tails = 0, heads = 0; for (counter = 1; counter <= 100; counter++)
1
2911
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 button. Once pulling down the drive list and clicking to the specific drive, folders will be shown in the dir list. When clicking the folders in the dir list, files in that certain folder should apprear in the file list, but this program should limit...
8
24926
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 should be return to the main program. I'm having trouble figuring it out, can anyone please help? #include <iostream> using namespace std; # include <ctime> int coin();
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10546
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9121
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.