Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 13th, 2004, 6:25 PM   #1
Corvus
Newbie
 
Join Date: Dec 2004
Posts: 2
Rep Power: 0 Corvus is on a distinguished road
G'day all

I am working my way through a book called "Python programming for absolute beginners" (I have very little programming experience) and at the end of one chapter you are asked to write a simple coin toss programme that tosses a coin 100 times and prints the result of each toss either as "heads" or "tails" using a while loop. I can get the random to work without the loop but I can't get it working properly using the loop, each toss is exactly the same (eg 100 instances of "heads"), though randomly it will change to the other and every time I run the programme for some time it will generate that.

Here is the code:

# Coin Toss programme
# Flips 100 coins and reports heads or tails

# Stuart Graham 10/12/04

import random
coin = random.randrange(2)

count = 0
toss = int(raw_input("enter number of tosses "))

while (count < toss): *
 *if (coin == 0):
 * *print "heads", coin
 *else: #(coin == 1)
 * *print "tails", coin

 *count += 1

While fiddling I have tried things like having the count += 1 directly after the while and I have added the toss to see if it makes any difference. It hasn't, beyond allowing me to change the number of coins tossed. I have tried coin = random.randrange(0.0, 1.0). Everything I have tried ends up with the same result.

Any hints would be appreciated as this is bugging the living daylights out of me.
Corvus is offline   Reply With Quote
Old Dec 13th, 2004, 6:54 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Put the coin = random... statement in the while loop. In your code, it's only being called once. You want it to be called every time.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 13th, 2004, 7:27 PM   #3
Corvus
Newbie
 
Join Date: Dec 2004
Posts: 2
Rep Power: 0 Corvus is on a distinguished road
D'oh! Thank you, thank you, thank you! It works perfectly now, I wouldn't have thought of that at all. I will have to remember it for future programmes.

Thanks again :rock:
Corvus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC