[英]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. 今天我用Python 3.2.2编写了一个小脚本,这个简单的代码决定给我带来麻烦。
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: 现在,这个代码应该是非常明显的,但是当我在Windows 7上运行它时,它工作得非常好,或者当我双击桌面上的脚本图标并打开它时,天气与否无关紧要我输入“y”它关闭,当然这是一个简单的解决方案:
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? 你确定你使用的是3.2.2而不是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. 在Windows上的Python 3.2.0中有一个错误,从stdin读取有时会在字符串的末尾留下
\\r
,这将解释你所看到的内容。 Use yn.strip().lower()
to workaround the bug or update to the current version (3.2.3). 使用
yn.strip().lower()
来解决错误或更新到当前版本(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. 具体问题描述为http://bugs.python.org/issue11272 ,但如果您使用的是3.2.2,则应该已修复。
It works for me on OSX (Python 3.2.2): 它适用于OSX(Python 3.2.2):
yn = ""
while True:
yn = input( "---> " )
if yn.lower() != "y":
break
pu@pumbair:~$ python3.2 test.py
---> y
---> y
---> Y
---> Y
--->
pu@pumbair:~$ python3.2 test.py
---> n
pu@pumbair:~$
What executable is associated with the .py extension? 什么可执行文件与.py扩展名相关联?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.