简体   繁体   English

Python 3.2.2在IDLE中运行时与在桌面上运行时的行为不同

[英]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.

相关问题 Python - 如何从 IDLE 与命令提示符运行模块 - Python - How to run module from IDLE vs. command prompt 从IDLE或命令行运行时,Web.py的行为有所不同 - Web.py behaves differently when run from IDLE or command line 为什么 python 脚本在 pycharm 中运行和在命令提示符下运行时的行为不同? - Why does a python script behaves differently when it is run in pycharm and when it is run from a command prompt? 从文件夹或桌面打开时,Python程序无法正确运行,但在IDLE中运行时,它可以正常运行 - Python program won't run correctly when opened from folder or desktop, but works fine when run in IDLE Python 2 pdb:在pdb提示符下运行时,语句的行为有所不同 - Python 2 pdb: a statement behaves differently when run at the pdb prompt 为什么 python 控制台运行与空闲不同 - why does python console run differently to idle 烧瓶运行与 python - flask run vs. python 为什么本地和远程运行webbrowser lib时行为不同? - Why is webbrowser lib acting differently when run locally vs. remotely? Python脚本可在IDLE中运行,但从CMD或Eclipse运行时不起作用 - Python script working in IDLE, but not working when run from CMD or Eclipse 当我从 IDLE 运行但不在命令行中运行时,Python 脚本有效吗? - Python script works when I run from IDLE but not in command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM