Why doesn't this work ? For loop variable scoping ?

Discussion in 'Python' started by Linuxguy123, Mar 19, 2009.

  1. Linuxguy123

    Linuxguy123 Guest

    Hi people.

    I've got a small piece of code that I don't understand. Basically, a
    variable inside an if statement inside a for loop doesn't seem to be
    updating. Is this a scope issue ?

    Thanks

    Code segment:

    <snip>

    # run through the cycle and calculate the temperature and pressure at
    each position, ie every 0.5 crankshaft degrees
    #global newCylinderPressure
    #global newCylinderTemperature

    for position in range(1,721):
    newCylinderPressure = cylinderPressure[position -1] *
    (cylinderVolume[position -1] / cylinderVolume[position])**1.4
    newCylinderTemperature = (cylinderTemperature[position -1] + 459.67)
    * (cylinderVolume[position -1] / cylinderVolume[position])**0.4 - 459.67

    # add 1.0 BTUs of heat to the combustion chamber at TDC, which is
    position # 360
    if position == 360:
    #calculate the new temperature
    print newCylinderTemperature
    deltaTemp = 1.0 / (airMass * Cv)
    newCylinderTempertature = newCylinderTemperature + deltaTemp
    print deltaTemp
    print newCylinderTemperature
    .....


    The output from this code is:

    1357.65862978 <-- newCylinderTemperature when we enter the if statement
    2626.04165688 <-- deltaTemp, as it should be
    1357.65862978 <-- newCylinderTemperature at the end of the if statement,
    which HASN'T CHANGED. It should be 1357 + 2626 = 3983. Why doesn't it
    change ? Is there some sort of scope issue here ?
     
    Linuxguy123, Mar 19, 2009
    #1
    1. Advertising

  2. Linuxguy123

    Aahz Guest

    In article <>,
    Linuxguy123 <> wrote:
    >
    >I've got a small piece of code that I don't understand. Basically, a
    >variable inside an if statement inside a for loop doesn't seem to be
    >updating. Is this a scope issue ?


    Nope, it's a spelling issue. I suggest you change your code to a more
    readable:

    newTemp += delta
    --
    Aahz () <*> http://www.pythoncraft.com/

    "Programming language design is not a rational science. Most reasoning
    about it is at best rationalization of gut feelings, and at worst plain
    wrong." --GvR, python-ideas, 2009-3-1
     
    Aahz, Mar 19, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    909
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,993
    Smokey Grindel
    Dec 2, 2006
  3. Archie

    Why doesn't this loop work?

    Archie, Jun 25, 2010, in forum: C++
    Replies:
    6
    Views:
    310
    Jonathan Lee
    Jun 25, 2010
  4. [Mr.] Lynn Kurtz

    Why doesn't this for loop work?

    [Mr.] Lynn Kurtz, Oct 28, 2007, in forum: Javascript
    Replies:
    5
    Views:
    106
    [Mr.] Lynn Kurtz
    Oct 29, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    383
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page