Question about Python syntax

Why won't the program work with this declared here, but work when

Page 1 of 1

5 Replies - 1244 Views - Last Post: 15 August 2010 - 12:46 PM Rate Topic: -----

#1 yunusabd  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 104
  • Joined: 25-October 08

Question about Python syntax

Posted 15 August 2010 - 10:14 AM

Hi all,

The code below works just as I intended, but for the past hour I was scratching my head because I was declaring coin outside of the while loop
see code comments

# set the initial values

counter = 0
coin = random.randrange(2)   #this is where I originally had the declaration but it did not work
heads = 0
tails = 0



******************************Working code***************
# Flip A Coin
# Flip a coin 100 times
# Heads or tails

import random


# set the initial values

counter = 0

heads = 0
tails = 0

while counter <100:
    
    coin = random.randrange(2) # so I moved it here and now it works
    print(coin)

    if coin == 0:
        heads += 1

    elif coin == 1:
        tails += 1

    else:
        print("You dropped your coin")
    counter += 1
        

print("The coin landed on heads", heads, "times and on tails ", tails, "times")
  

input("\n\nPress the enter key to exit.")





I want to know why I have to declare and initialize the 'coin' but not the other variables. In addition I would like to know what other types need to be called inside a loop

Is This A Good Question/Topic? 0
  • +

Replies To: Question about Python syntax

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Question about Python syntax

Posted 15 August 2010 - 10:22 AM

If you put random.randrange() inside the while loop, random values of 0 and 1 are assigned to coins variable every time the loop runs. However, if it's outside the while loop, it will be assigned one value, let's say tails, and then in the while loop, you'll get 100 tails and 0 heads.
Was This Post Helpful? 0
  • +
  • -

#3 yunusabd  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 104
  • Joined: 25-October 08

Re: Question about Python syntax

Posted 15 August 2010 - 10:33 AM

View PostCuzzie, on 15 August 2010 - 09:22 AM, said:

If you put random.randrange() inside the while loop, random values of 0 and 1 are assigned to coins variable every time the loop runs. However, if it's outside the while loop, it will be assigned one value, let's say tails, and then in the while loop, you'll get 100 tails and 0 heads.



I understand that coin has to be inside the loop I just don't understand why the full declaration needs to be
coin = random.randrange=(2)

while counter < 100:
    coin
    counter += 1



My question is why will the above code not work?
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Question about Python syntax

Posted 15 August 2010 - 11:19 AM

View Postyunusabd, on 15 August 2010 - 09:33 AM, said:

View PostCuzzie, on 15 August 2010 - 09:22 AM, said:

If you put random.randrange() inside the while loop, random values of 0 and 1 are assigned to coins variable every time the loop runs. However, if it's outside the while loop, it will be assigned one value, let's say tails, and then in the while loop, you'll get 100 tails and 0 heads.



I understand that coin has to be inside the loop I just don't understand why the full declaration needs to be
coin = random.randrange=(2)

while counter < 100: 
    coin
    counter += 1



My question is why will the above code not work?


The "coin" variable is only evaluated once in this code.

The first run, for example:


coin = 0

while counter < 100: # counter = 0

coin # which is 0
counter += 1




The second loop:

coin = 0 # This is not evaluated again because the "while" loop isn't finished

''' The flow of code "jumps" back to here so any assignments above do not get evaluated'''

while counter < 100: # counter = 1

coin # is still the same
counter += 1



If you place:

coin = random.randrange(2)


In the while loop, it is evaluated every time the code is in the looping construct (while counter < 100) or else it is only evaluated once.

The assignment to the variable "coin" must be included in the loop so it is reassigned each time the code loops.

This is what you have:

 coin = 1 or 0

while count is 0, 1, 2, 3 .... etc

coin
counter += 1




You need to have:

while count is 0, 1, 2, 3, ... etc

coin = 1 or 0
counter += 1




In the first example the random variable is only evaluated once.

Is this clearer?

This post has been edited by Simown: 15 August 2010 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

#5 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Question about Python syntax

Posted 15 August 2010 - 11:27 AM

Sorry, I don't really get what you're asking here...

Quote

I want to know why I have to declare and initialize the 'coin' but not the other variables.


You did declare and initialize other variables(counter, tails, heads). Initialization of variables are needed if the variables will be involved in mathematical operations later in the code, thus assigning 0 to counter, tails and heads is correct. However, you don't have to initialize "coin", because in the while loop, every time the loop runs, different random number ranging from 0 to 1 will be assigned to it.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Question about Python syntax

Posted 15 August 2010 - 12:46 PM

Understand that each call to random.randrange(2) produces a result. So you need to call it 100 times to get 100 different results.

You could have don't the same thing with this, if it makes more sense.
flip = [0,0]
for i in range(100): 
	flip[random.randrange(2)] += 1

print ("The coin landed on heads %d times and on tails %d times." % (flip[0], flip[1]))


This post has been edited by baavgai: 15 August 2010 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1