By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

another beginner game question

P: 15
Hi all

Its me again. This time I want to create a program that flips a coin 100 times
and tells you the number of heads and tails

I tried using an intentional infinite loop but it didn't turn out right
please give some advice

thanks
Mar 11 '08 #1
Share this Question
Share on Google+
7 Replies


P: 43
Hi all

Its me again. This time I want to create a program that flips a coin 100 times
and tells you the number of heads and tails

I tried using an intentional infinite loop but it didn't turn out right
please give some advice

thanks
Can you post some code. Use a for loop for the loop.
Expand|Select|Wrap|Line Numbers
  1. def flipCoin():
  2.     #...
  3.  
  4. for i in xrange(100):
  5.     print flipCoin()
  6.  
Mar 12 '08 #2

P: 15
Can you post some code. Use a for loop for the loop.
Expand|Select|Wrap|Line Numbers
  1. def flipCoin():
  2.     #...
  3.  
  4. for i in xrange(100):
  5.     print flipCoin()
  6.  


Well I first tried to make it print out the 100 times of coin flip

Expand|Select|Wrap|Line Numbers
  1. import random
  2. tries = 1
  3. while True:
  4.     side = random.randrange(2)
  5.     tries += 1
  6.     if tries > 100:
  7.         break
  8.  
  9. if side == 0:
  10.     print "Heads"
  11. elif side == 1:
  12.     print "tails"
  13. else:
  14.     print "None"
  15. print side
  16.  
  17.  
  18. raw_input("\n\nPress the Enter key to exit")

but it didn't turn out so well
Mar 12 '08 #3

bgeddy
P: 16
Well going through your code - here are some observations.

Firstly you do not accumulate the numer of heads and tails occurring so you have no count to show. Secondly, as stated, if you are simply doing something a set number of times in a loop a 'for num in xrange(loops)'
is the simplest construct.

Even if using a while loop as you have, you don't need an infinite loop and a break, in your case "while tries<=100" would be better. Then you drop the indentation so only the last side tossed will be printed and as side can only be 0 or 1 you don't need the 'else : print 'None'.

The code here may give you some ideas - I've included a dictionary, a tuple and a subscript and an augmented assignment when they're not strictly necessary to give you some things to check up on !

Expand|Select|Wrap|Line Numbers
  1. import random
  2. tosses={"Heads":0,"Tails":0}
  3. for i in xrange(100):
  4.     tosses[("Heads","Tails")[random.randrange(2)]]+=1
  5. print tosses
Hope this helps..
Mar 12 '08 #4

P: 15
thanks alot for you time.

and what does xrange do?
Mar 12 '08 #5

jlm699
100+
P: 314
and what does xrange do?
Same thing as range() only instead of returning a list it's more of an as-needed int ... it's better for memory efficiency and faster than range in loops..
Mar 12 '08 #6

bgeddy
P: 16
I'm not sure what is this site's policy on giving URL's so I'll just say if you were to google "python411" you will find a site with loads of tutorials/books/links etc for python.

Read up as much as you can but always have a python interpreter fired up to play with ! You will learn much more by experimentation of concepts.

At the python prompt you can type :

Expand|Select|Wrap|Line Numbers
  1. help(xrange) 
to get an idea what this does (althought you may find the information cryptic at first).

Good Luck !!
Mar 13 '08 #7

P: 15
thanks alot. The website sure helped me
Mar 13 '08 #8

Post your reply

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