Python 代码在 IDLE 和 Eclipse 之间的行为不同

发布于 2024-10-18 12:28:16 字数 1633 浏览 1 评论 0原文

我对编程完全陌生,所以我决定自学Python。在阅读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: 

:代码本身有问题导致这种情况还是只是一些奇怪的交互?任何帮助将不胜感激。

I'm completely new to programming, so I decided to teach myself python. While going through the Non-Programmer's Tutorial for Python 3, I stumbled across a strange occurrence. When I run this code:

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: ')

The resulting output is what you'd expect:

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

But when I run the same code on Eclipse (IDLE was crashing so I tried something else), the program just loops instead of going into the other options:

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: 

Is there something wrong in the code itself to cause this or is it just some wonky interaction? Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

莫多说 2024-10-25 12:28:16

我认为该程序看起来不错。也许 Eclipse 处理输入的方式存在错误。您使用的是最新版本的 PyDev 吗?我建议你尝试另一个 IDE(例如 PyCharm)。

The program looks ok in my opinion. Perhaps there is a bug with how Eclipse handle input. Are you using the latest version of PyDev? I suggest you try another IDE (like PyCharm).

陈独秀 2024-10-25 12:28:16

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

I`ve tried to run your code in IDLE and all is ok, in eclipse too. Try to reinstall last version of PyDev

痴情换悲伤 2024-10-25 12:28:16

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

在这两行之间:

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

添加以下内容,缩进与 if choice == '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),请在这两个工具中为示例代码执行“显示空白”,并验证代码确实与 Eclipse/PyDev 正在运行的代码相同。

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

干杯,

If the latest Eclipse/Pydev doesn't fix the issue, I'd recommend that you add a couple of debug/display lines to show you what Eclipse thinks it received as input.

Between these two lines:

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

Add the following, indented at the same level as the if choice == 'c': line:

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

If debug2 is not 1 or debug3 is not True, there's a sneaky gotcha with whitespace which could probably be resolved with changing this:

while choice != 'q':

to this:

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

Also, I am curious about the description. You said the code works --- where did it work? You said IDLE crashed and Eclipse does not work.

If you're using another editor (like Notepad++, PyScripter) or a different IDE (like PyCharm or WingIDE), do a Show Whitespace for the example code in both tools and verify that the code is indeed identical to what Eclipse/PyDev is running.

In Python, whitepsace (tabs, spaces) definitely matters and some tools interpret them slightly different --- especially in cases where you bounce back and forth between them.

Cheers,
Rob

懒的傷心 2024-10-25 12:28:16

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

If you run python in your terminal you will find it to be more accurate. In fact, I use eclipse and run everything in python launcher.

赠意 2024-10-25 12:28:16

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

try running the code through your terminal-- $ python3 filename.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文