Python 代码在 IDLE 和 Eclipse 之间的行为不同
我对编程完全陌生,所以我决定自学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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为该程序看起来不错。也许 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).
我尝试在 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
如果最新的 Eclipse/Pydev 不能解决问题,我建议您添加几行调试/显示行,以显示 Eclipse 认为它收到的输入内容。
在这两行之间:
添加以下内容,缩进与 if choice == 'c': 行相同:
如果 debug2 不是 1 或 debug3 不是 True,则存在一个带有空格的偷偷摸摸的问题,可以通过更改来解决这:
对此:
另外,我对描述很好奇。你说代码有效——它在哪里有效?你说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:
Add the following, indented at the same level as the if choice == 'c': line:
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:
to this:
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
如果你在终端中运行 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.
尝试通过终端运行代码 -
$ python3 filename.py
try running the code through your terminal--
$ python3 filename.py