【问题标题】:Python code behaving differently between IDLE and EclipsePython 代码在 IDLE 和 Eclipse 之间的行为不同
【发布时间】:2011-02-22 04:58:38
【问题描述】:

我对编程完全陌生,所以我决定自学 Python。在浏览the Non-Programmer's Tutorial for Python 3 时,我偶然发现了一件奇怪的事情。当我运行这段代码时:

def print_options():
    print('Options:')
    print(" 'p' print options")
    print(" 'c' convert from Celsius")
    print(" 'f' convert from Fahrenheit")
    print(" 'q' quit")

def c_to_f(c_temp):
    return 9.0/5.0 * c_temp + 32

def f_to_c(f_temp):
    return (f_temp-32)*5.0/9.0

choice = 'p'

while choice != 'q':
    if choice == 'c':
        temp = float(input('Celsius temp: '))
        print('Fahrenheit:', c_to_f(temp))
    elif choice == 'f':
        temp = float(input('Fahrenheit temp:'))
        print('Celsius:', f_to_c(temp))
    elif choice == 'p':
        print_options()
    choice = input('Options: ')

结果输出是你所期望的:

Options:
 'p' print options
 'c' convert from Celsius
 'f' convert from Fahrenheit
 'q' quit
Options: f
Fahrenheit temp:98.6
Celsius: 37.0
Options: c
Celsius temp: 37
Fahrenheit: 98.60000000000001
Options: q

但是当我在 Eclipse 上运行相同的代码时(IDLE 崩溃了,所以我尝试了其他方法),程序只是循环而不是进入其他选项:

Options:
 'p' print options
 'c' convert from Celsius
 'f' convert from Fahrenheit
 'q' quit
Options: c
Options:
 'p' print options
 'c' convert from Celsius
 'f' convert from Fahrenheit
 'q' quit
Options: f
Options:
 'p' print options
 'c' convert from Celsius
 'f' convert from Fahrenheit
 'q' quit
Options: q
Options:
 'p' print options
 'c' convert from Celsius
 'f' convert from Fahrenheit
 'q' quit
Options: 

是代码本身有什么问题导致了这种情况,还是只是一些不稳定的交互?任何帮助将不胜感激。

【问题讨论】:

    标签: eclipse python-3.x pydev


    【解决方案1】:

    在我看来,该程序看起来不错。 Eclipse 处理输入的方式可能存在错误。您使用的是最新版本的 PyDev 吗?我建议您尝试其他 IDE(例如 PyCharm)。

    【讨论】:

      【解决方案2】:

      我尝试在 IDLE 中运行您的代码,一切正常,在 eclipse 中也是如此。尝试重新安装最新版本的 PyDev

      【讨论】:

        【解决方案3】:

        如果最新的 Eclipse/Pydev 不能解决问题,我建议您添加一些调试/显示行,以向您展示 Eclipse 认为它作为输入接收到的内容。

        这两行之间:

        while choice != 'q':
            if choice == 'c':
        

        添加以下内容,缩进与 if 选择 == 'c': 行:

        print ('debug1\tchoice =', choice)                # what you typed
        print ('debug2\tlen(choice)=', len(choice))       # should be length of 1,
        print ('debug3\tsame?', choice == choice.strip()) # should be True
        

        如果 debug2 不是 1 或 debug3 不是 True,则有一个带有空格的鬼鬼祟祟的陷阱,可以通过更改它来解决:

        while choice != 'q':
        

        到这里:

        while choice.strip() != 'q':
        

        另外,我很好奇描述。你说代码有效——它在哪里有效?你说 IDLE 崩溃了,Eclipse 不工作了。

        如果您使用其他编辑器(如 Notepad++、PyScripter)或不同的 IDE(如 PyCharm 或 WingIDE),请在两个工具中为示例代码执行 Show Whitespace 并验证代码确实与 Eclipse/ PyDev 正在运行。

        在 Python 中,whitepsace(制表符、空格)确实很重要,一些工具对它们的解释略有不同——尤其是在您在它们之间来回切换的情况下。

        干杯, 抢

        【讨论】:

          【解决方案4】:

          如果您在终端中运行 python,您会发现它更准确。事实上,我使用 eclipse 并在 python 启动器中运行所有内容。

          【讨论】:

            【解决方案5】:

            尝试通过您的终端运行代码--$ python3 filename.py

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-10-11
              • 2017-07-21
              • 1970-01-01
              • 1970-01-01
              • 2014-07-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多