440,345 Members | 1,783 Online
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
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 def flipCoin():     #...   for i in xrange(100):     print flipCoin()   Mar 12 '08 #2

 P: 15 Can you post some code. Use a for loop for the loop. Expand|Select|Wrap|Line Numbers def flipCoin():     #...   for i in xrange(100):     print flipCoin()   Well I first tried to make it print out the 100 times of coin flip Expand|Select|Wrap|Line Numbers import random tries = 1 while True:     side = random.randrange(2)     tries += 1     if tries > 100:         break   if side == 0:     print "Heads" elif side == 1:     print "tails" else:     print "None" print side     raw_input("\n\nPress the Enter key to exit") but it didn't turn out so well Mar 12 '08 #3

 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 import random tosses={"Heads":0,"Tails":0} for i in xrange(100):     tosses[("Heads","Tails")[random.randrange(2)]]+=1 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

 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

 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 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