# Coin flip game

5 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Coin flip game

 Hi,   I’m new to the list and to Python. I’m reading through Michael Dawson’s ‘Python programming: for absolute beginners’ and at the end of chapter 3 he’s set a challenge where the reader has to create a coin flip game. My code now works, but only after I randomly switched pieces of the code around and, basically, pulled my hair out because it wouldn’t work.   My code is below. But can someone please explain to me why the following variable has to be placed where it is for the code to work? I thought it would need to go nearer the start of the code i.e. just before heads = 0, tails = 0 etc:                   coin = random.randrange(2)   Also, why does the randrange integer have to be ‘2’? I only discovered this worked by complete accident. I tried ‘1’ and ‘0,1’ as my integers but they just didn’t work.   Thanks,   Lawrence     import random print "The Coin Flip Game\n"   heads = 0 tails = 0 count = 0   while count < 100:     coin = random.randrange(2)     if coin == 0:         heads = heads + 1     else:         tails = tails + 1     count += 1     print "Heads: ", heads print "Tails: ", tails   raw_input("\nPress enter to exit.")       _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Coin flip game

 On Fri, Feb 12, 2010 at 7:15 AM, Jones, Lawrence D <[hidden email]> wrote: > My code is below. But can someone please explain to me why the following > variable has to be placed where it is for the code to work? I thought it > would need to go nearer the start of the code i.e. just before heads = 0, > tails = 0 etc: >                 coin = random.randrange(2) If you put this at the start of the code (before the loop), then you only flip the coin once, and then count that single flip 100 times. That would work, but wouldn't be a very useful program. > Also, why does the randrange integer have to be ‘2’? I only discovered this > worked by complete accident. I tried ‘1’ and ‘0,1’ as my integers but they > just didn’t work. See: http://docs.python.org/library/random.html#random.randrangerandom.randrange parameters are the same as for range, which you can learn more about here: http://docs.python.org/tutorial/controlflow.html#the-range-functionHTH, benno _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Coin flip game

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Coin flip game

 In reply to this post by Jones, Lawrence D "Jones, Lawrence D" <[hidden email]> wrote > My code is below. But can someone please explain to me > why the following variable has to be placed where it is Others have explained about variable creation and the fact you need to be inside the loop to get different results for each iteration.                 coin = random.randrange(2) I just want to pick up on something you said. You asked about the "variable". The variable is "coin". It can go anywhere before the point of use, even the first line of your code. You could have done coin = None or coin = 0 and it would work just fine. What needs to be inside the loop is the call to the randrange() function. That is what is simulating the coin flip. So you need to distinguish the difference between variable creation (which in Python happens by means of the first assignment of a value)  and function application (where you call a function and assign its return value to a variable.) In your code you create the variable coin at the same time as you apply the function, but you could have done those two things separately and the code would still work. It might seem like I'm splitting hairs but it starts to make a difference in some other cases, like this: while True:     coin = coin + someFunction() This will raise an error because you are using the value of coin (on the right hand side) before it has been created. You need to write it like this: coin = 0 while True     coin = coin + someFunction() It is very important in programming to be clear in your mind about these different concepts, especially when deciphering error messages. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/_______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Coin flip game

In reply to this post by David-591