473,325 Members | 2,608 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,325 software developers and data experts.

beginner code problem

RJ
I'm trying to teach myself Python (probably running into the old dog
new tricks issue) and I'm trying to start with the very basics to get a
handle on them.

I'm trying to write code to get the computer to flip a coin 100 times
and give me the output of how many times heads and tails. After solving
a few syntax errors I seem to be stuck in an endless loop and have to
kill python. A few times I would get it to print 'heads 0 (or 1) times
and tails 1 (or 0) times' 100 times.

Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:

if flip == 0:
heads += 1

else:
tails += 1
count += 1

print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

Jun 2 '06 #1
6 1646
On 3/06/2006 8:41 AM, RJ wrote:
I'm trying to teach myself Python (probably running into the old dog
new tricks issue) and I'm trying to start with the very basics to get a
handle on them.

I'm trying to write code to get the computer to flip a coin 100 times
and give me the output of how many times heads and tails. After solving
a few syntax errors I seem to be stuck in an endless loop and have to
kill python.
This can't happen with the code that you posted. It *could* happen if
the statement count += 1 was not being executed once each time around
the while loop -- like if it was *not* indented.

# Bad code #1
import random
flip = random.randrange(2)
heads = tails = count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'
A few times I would get it to print 'heads 0 (or 1) times
and tails 1 (or 0) times' 100 times.
Again, can't happen with the code you have posted. If it is printing 100
times, that would be because you have indented the print statement so
that it is being executed once each trip around the loop.

# Bad code #2
import random
flip = random.randrange(2)
heads = tails = count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:
To help you see what is happening, insert a print statement here; e.g.:
print flip, count, heads, tails
if flip == 0:
heads += 1

else:
tails += 1
count += 1

print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'


The code that you posted sets flip only once i.e. only 1 toss, not 100.
If it is 0, you get 100 heads and 0 tails. Otherwise you get 0 heads and
100 tails. You need to get a new value for flip each trip.

# Not quite so bad code
import random
heads = tails = count = 0
while count < 100:
flip = random.randrange(2)
# print flip, count, heads, tails # un-comment as/when required :-)
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

HTH,
John
Jun 2 '06 #2
Hello
Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:

if flip == 0:
flip never changes again
it's not reassigned in the while loop
heads += 1

else:
tails += 1
count += 1

print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

in case you know how many times to iterate
it's better to use for loop (in python and eq C also)

from random import randrange as flip
result = [0,0]
for i in range(100):
result[flip(2)] += 1

or

from random import randrange as flip
result = {"head":0,
"tail":0}
for i in range(100):
result[["head","tail"]flip(2)] += 1

or
class Coin:

.... def flip(self):
.... import random
.... return ("head", "tail")[random.randrange(2)]
c = Coin()
result = {"head":0,"tail":0}
for i in range(100):
result[c.flip()] += 1

or many many more
the important thing is .. to know what is the
most suitable data representation for you

is it throw-away-code or is this going to be read
by other people .. etc

hth, Daniel
Jun 2 '06 #3
On Fri, 2 Jun 2006 18:41:53 -0400, RJ <vi***********@yahoo.com> wrote:
I'm trying to teach myself Python (probably running into the old dog
new tricks issue) and I'm trying to start with the very basics to get a
handle on them.

I'm trying to write code to get the computer to flip a coin 100 times
and give me the output of how many times heads and tails. After solving
a few syntax errors I seem to be stuck in an endless loop and have to
kill python. A few times I would get it to print 'heads 0 (or 1) times
and tails 1 (or 0) times' 100 times.

Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:

if flip == 0:
heads += 1

else:
tails += 1
count += 1

print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'


Several problems here. "flip" is defined just once, so you'll either have
100 heads or tails and your whitespace is all wrong - that's important in
Python. Here's how it should look:

import random

def coinflip():
heads = 0
tails = 0
for goes in range(100):
if random.randrange(2) == 1:
heads += 1
else:
tails += 1
print "heads", heads
print "tails", tails

if __name__ == "__main__":
coinflip()

Jun 2 '06 #4
On 2006-06-02 19:25:28 -0400, John Machin <sj******@lexicon.net> said:
Thanks for the reply John. I seem to be getting all the same problems
with your code that I had with mine so it may be an issue with Python
on this computer though I haven't had one prior to now. I'm running it
on OSX Tiger so I'll give it a shot on my Windows box. With pythons
portability I didn't think it would be an issue so didn't mention it in
the post.
The different problems I was having was a result of moving some of
the code around thinking I had messed up and trying to fix it. The code
I posted was just running but never stopping to give the the output.

Thanks for explaining about getting a new value for flip, I wasn't
positive about that and you really helped clear up any confusion I was
having.

Rob

This can't happen with the code that you posted. It *could* happen if
the statement count += 1 was not being executed once each time around
the while loop -- like if it was *not* indented.

# Bad code #1
import random
flip = random.randrange(2)
heads = tails = count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'
A few times I would get it to print 'heads 0 (or 1) times and tails 1
(or 0) times' 100 times.


Again, can't happen with the code you have posted. If it is printing
100 times, that would be because you have indented the print statement
so that it is being executed once each trip around the loop.

# Bad code #2
import random
flip = random.randrange(2)
heads = tails = count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

Here's the code I wrote:

import random

flip = random.randrange(2)
heads = 0
tails = 0
count = 0

while count < 100:


To help you see what is happening, insert a print statement here; e.g.:
print flip, count, heads, tails

if flip == 0:
heads += 1
else:
tails += 1

count += 1

print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'


The code that you posted sets flip only once i.e. only 1 toss, not 100.
If it is 0, you get 100 heads and 0 tails. Otherwise you get 0 heads
and 100 tails. You need to get a new value for flip each trip.

# Not quite so bad code
import random
heads = tails = count = 0
while count < 100:
flip = random.randrange(2)
# print flip, count, heads, tails # un-comment as/when required :-)
if flip == 0:
heads += 1
else:
tails += 1
count += 1
print "The coin landed on heads", heads, 'times ' \
"and tails", tails, 'times'

HTH,
John

Jun 3 '06 #5
RJ:
import random
flip = random.randrange(2)
heads = 0
tails = 0
count = 0
while count < 100:
if flip == 0:
heads += 1
else:
tails += 1
count += 1


Since flip isn't changed in the loop, this is going to report 100 heads or
100 tails, depending on the zeroness of flip.

--
René Pijlman
Jun 3 '06 #6
RJ
Thanks for all the help and examples. As I mentioned, I'm trying to
teach myself and starting with the basics (if, elif, else, while, for,
raw_input, int are about all I know so far and don't know that well
yet) Some of the example posted are still beyond my understanding but
it's good to see other ways of acheiving the result and other things
to look up and read about.

I've read a bit about Python but only really started trying
yesterday. Right now I'm still strugling but with practice I hope for
it to come easier and I'll keep learning more syntax.

I didn't even think about random.randrange being out of the loop but
it makes sence now. Chalk it up to a newbie mistake and a learning
experience.

I really do appreciate the help and I'm sure this is only the first
of many questions I'll have. It's good to know that there are others
who can help me understand my mistakes.

Rob
Jun 3 '06 #7

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

Similar topics

3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
1
by: Phil | last post by:
Hi I admit it, I am a total vb beginner. I thought I'd get hold of a copy to see if I could resolve a simple problem we have. I am looking to search AD for user objects for reporting purposes,...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
4
by: Bails | last post by:
Hi Im an absolute beginner in programming and am using VB.Net Express. To start my larning I decided to do a "Real World" app instead of "hello world" and am creating a Poker Countdown clock. ...
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
1
by: H. Fraser | last post by:
I've downloaded all of the code and video dealing with the RSS Reader but when i try to run it, it throws an exception, saying 404 page not found. And, As a total beginner, i've no idea where...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.