#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Dumb beginner question


    This produces the expected output:
    Code:
    a = 0
    while a < 10:
        print(a)
        a+=1
    But this creates an infinite loop:
    Code:
    a = 0
    while a < 10:
        print(a)
        a+1
    In the latter case, the output is alternately 0 and 1 and it continues infinitely (or would have continued until my computer ran out of RAM and swap space if I hadn't keyboard interrupted).

    Why?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    the expression a + 1 doesn't actually do anything. It's just a mathematical expression like 1 + 1 or 2 * 3.

    a += 1, on the other hand, is a statement: It takes the value of the variable a, adds 1 to it and stores the result in the same variable. So in the end, the variable has been increased by 1.

    Those are two completely different things. It'll become clearer if you use an explicit assignment instead of the short += syntax:

    In the first loop, you have the assignment
    Code:
    a = a + 1
    In the second loop, you have the (useless) expression

    Code:
    a + 1
    You said that a switches between 0 and 1. That's not true -- unless you have some other code below the one you showed us. The variable a never changes its value, it's always 0.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0
    The actual code was this:

    Code:
    >>>def some_stupid_function(a):
    ... while a < 10:
    ...  print(a)
    ...  a+1
    ...
    >>>some_stupid_function(0)
    0
    1
    0
    1
    0
    1
    0
    1
    ...
    ctrl-c
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    The only way you could get "1" is that you're running Python in some kind of interactive mode which displays the value of each expression. But it's not the output of print. You've set a to 0 at the beginning, and you never change it. So its value is and stays 0. That's one of the most fundamental truths I could think of.

    Run Python from the command line:

    Code:
    python yourscript.py
    And then the only output you should ever see is 000000000... Otherwise, there's something very, very wrong with your Python installation or your computer.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0
    I tested on another system under both python 2.7 and python 3.3. This is what happened

    >>>a = 0
    >>>while a < 10:
    ... print(a)
    ... a + 1
    ...
    1
    0
    1
    0
    ...

    But then I tried this:

    >>>a = 0
    >>>while a < 10:
    ... print(a)
    ... a + 1
    ... print(a)
    ...
    1
    0
    0
    1
    0
    0
    ...

    Totally different flavor of mint, but similar hardware. I have no idea what's going on.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You're running in the interpreter. It prints each result unless that result is None or the statement is an assignment statement.



    regarding an earlier post, a statement is a logical line.
    http://docs.python.org/3/reference/lexical_analysis.html#line-structure
    Thus your statement a+1 is a statement, that doesn't do anything except waste time and assuming a is not an object (in this case it is not) with an __add__ method that has side effects. (it does not.)
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by RickBraun
    Totally different flavor of mint, but similar hardware. I have no idea what's going on.
    What line of your code would make you think you're changing the value of 'a'? As Jacques already explained, "a+1" doesn't change the value of 'a', it just adds one to a and throws the result away. Since you're in the interactive shell, it kindly prints the output before throwing it away, but it doesn't assign it to anything.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0
    I was able to verify Jacques' suggestion with a script, where the 1 did not get printed. Glad that's all cleared up.

    I have no idea what made me think that a+1 would do the same thing as a+=1. <shrug> Chalk it up to newbiness.

IMN logo majestic logo threadwatch logo seochat tools logo