473,394 Members | 1,813 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,394 software developers and data experts.

coin flip

I am creating a python program that will flip a coin 100 times and then tells you the number of heads and tails. This is for a class I am taking, I am not looking for the answer. I have already started writing the program, but something is missing because it will not run...can someone let me know if they can see my error?

import random
heads=0
tails=0
count=1
while count <101:
randomFlip = random.randrange(0,1)
if randomFlip == 0:
heads = heads + 1
else:
tails = tails + 1
count = count + 1
print heads," heads"
print tails," tails"
Oct 3 '08 #1
3 6083
boxfish
469 Expert 256MB
I see two things wrong with the code you have there. One is that not all the code in the while loop is indented enough. You need to have it be:
Expand|Select|Wrap|Line Numbers
  1. while count <101:
  2.     randomFlip = random.randrange(0,1)
  3.     if randomFlip == 0:
  4.         heads = heads + 1
  5.     else:
  6.         tails = tails + 1
  7.     count = count + 1
  8.  
Indent everything inside the loop, and if something is inside an if statement inside of a loop, it has to be indented twice.

The other thing is that random.randrange(0,1) will not pick either zero or one. It will choose a random integer that is greater than or equal to zero, but less than one. Not much of a choice. Your program picks all heads. Make it pick a random integer that is greater than or equal to zero but less than 2 with random.randrange(2).

And when you are posting code, please put [CODE] before it and [/CODE] after it, so it shows up in a nice little box.

Hope this helps.
Oct 3 '08 #2
Thanks for your input...it helped a lot!
Oct 3 '08 #3
Maybe you could look into the random.randint() function... play about with it in IDLE.

Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> help(random.randint)
  3. Help on method randint in module random:
  4.  
  5. randint(self, a, b) method of random.Random instance
  6.     Return random integer in range [a, b], including both end points.
  7.  
-freddukes
Oct 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
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...
0
by: coolindienc | last post by:
Honestly, I am totally lost about this program. I don't even know where to start. At first I thought I can ask a user to flip a coin. But then again one will get bored flipping coin fot 100 times....
8
by: coolindienc | last post by:
At first I had no idea where to start. Finally I tried to get somewhere and I am still stuck. I wrote two different ways using while loop and for loop. Below are codes for both. I am getting...
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...
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
Chuncatty
by: Chuncatty | last post by:
How much does a Draped Bust coin fabricate cost? (Sorry for my Eng) I ask a beau to see if it's genuine he said it is, down repay a collage professor i met said so. It's in virtue word,...
18
by: Chinde | last post by:
Hi I'm using AS2 to produce a simple platform game. So far so good I would like the character to collect coins or whatever. so what I have done is create a coin movie clip, I've run a hittest on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
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...

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.