分页: 1 / 1

[已解决]同样的代码为什么python解释器交互模式没问题,IDLE的Shell就提示语法错误?

发表于 : 2016-09-06 2:44
科学之子
同样的代码为什么python解释器交互模式没问题,IDLE的Shell就提示语法错误?
都是python3.4.2
IDLE提示:
SyntaxError: invalid syntax

代码: 全选

def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)
http://stackoverflow.com/questions/6843 ... 94#6843294
把需要粘贴的代码保存为py源文件,然后用IDLE打开,然后F5运行就可以了

Re: 同样的代码为什么python解释器交互模式没问题,IDLE的Shell就提示语法错误?

发表于 : 2016-09-06 6:35
vickycq
尝试分开粘贴,IDLE 貌似不支持一次粘贴多行代码

尝试粘贴以下代码看能否运行?

代码: 全选

a = 1
b = 2
c = 3
>>> a = 1
b = 2
c = 3
SyntaxError: multiple statements found while compiling a single statement
>>>
类似情况 参考 http://stackoverflow.com/questions/2502 ... ythontutor

Re: 同样的代码为什么python解释器交互模式没问题,IDLE的Shell就提示语法错误?

发表于 : 2016-09-06 7:03
科学之子
vickycq 写了:尝试分开粘贴,IDLE 貌似不支持一次粘贴多行代码

尝试粘贴以下代码看能否运行?

代码: 全选

a = 1
b = 2
c = 3
>>> a = 1
b = 2
c = 3
SyntaxError: multiple statements found while compiling a single statement
>>>
类似情况 参考 http://stackoverflow.com/questions/2502 ... ythontutor
好像在编辑器里面先粘贴代码,然后F5运行,就能正常了