Python-IDLE语法错误问题
我正在使用 IDLE 来学习 python,我刚刚注意到一些事情。每当我犯错并按回车键时,程序就会抛出语法错误。到现在为止都还好。但为什么我不能返回,编辑行,然后重新运行代码而不是再次编写?我的意思是:
>>> def func(params):
return ";"
if 1 + 1 == 2 """this will throw a syntax erro when I press enter"""
我不能只缩进并继续编码,而不必再次编写整个代码吗?
I am using IDLE to learn python and I just notices something. Whenever I do a mistake, and press enter key, the program throws a syntax error. That's fine till now. But why I cannot go back, edit the lines and then re-run the code instead of writing again? I mean:
>>> def func(params):
return ";"
if 1 + 1 == 2 """this will throw a syntax erro when I press enter"""
can't I just put an indent and continue to code instead of having to write the whole code again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您正在将程序输入 REPL - 读取、评估、打印循环。当您输入行时,它们将被评估并打印输出。
您应该将代码写入一个文件中,将其命名为 MyCode.py 或其他名称,然后使用 IDLE 打开该文件。然后,您可以从 IDLE 中调用文件内的函数,或者一次性执行整个过程。
It's because you are entering your program into an REPL - Read, Evaluate, Print Loop. As you enter lines, they are being evaluated and the output is printed.
You should write your code in a file, name it MyCode.py or whatever, and then open that with IDLE. You can then call functions inside your file from within IDLE, or execute the whole thing all at once.
您之前输入的内容保持不变,因此您可以准确记录您输入的内容和得到的结果。
如果您在 IDLE 中输入的一行有语法错误,并且您想要更正它,但又不想再次输入整个内容,那么您应该单击该行的末尾并按 Enter。然后,您将在 IDLE 窗口底部获得该行的新的可编辑副本。
The input you have put in before is kept unchanged so you have a record of exactly what you typed and what results you got.
If you typed a line into IDLE that had a syntax error and you want to correct it, but don't want to type the whole thing again, then you should click at the end of the line and press Enter. Then you'll get a new editable copy of the line at the bottom of the IDLE window.
如果您使用的是 Windows,我建议使用 PyScripter。这太神奇了,尤其是对于初学者来说。
If you're on Windows I suggest using PyScripter. It's amazing, especially for beginners.