Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.2.2 IF behaves differently when run in IDLE vs. run from desktop

Good day!

Today I was writing a small script with Python 3.2.2, and this simple piece of the code decided to give me trouble.

def main():
    yn = ""
    #...
    while True:
        #...
        yn = input( "---> " )
        if yn.lower() != "y":
            break

Now, it should be pretty obvious what this code does however when I run it in IDLE on Windows 7 it works perfectly fine, alternatively when I double click on the script's icon on my Desktop and open it, it does not matter weather or not I enter "y" it closes, of course this is an easy fix by writing:

if yn.lower() == "n":
   #...

which is what I did, however I was wondering what the cause of this could be?

like image 928
The Floating Brain Avatar asked Dec 19 '25 06:12

The Floating Brain


1 Answers

Are you sure you are using 3.2.2 rather than 3.2.0?

There's a bug in Python 3.2.0 on Windows that reading from stdin sometimes leaves a \r on the end of the string and that would explain what you're seeing. Use yn.strip().lower() to workaround the bug or update to the current version (3.2.3).

The specific issue is described as http://bugs.python.org/issue11272, but if you are using 3.2.2 it should have been fixed.

like image 59
Duncan Avatar answered Dec 21 '25 20:12

Duncan