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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With