简体   繁体   English

python spyder无法定义函数或运行循环

[英]python spyder can't define function or run loop

using python 3.6 with spyder when i try to run any code that has (:) it gives me this error 当我尝试运行具有(:)的任何代码时,将python 3.6与spyder一起使用会给我这个错误

SyntaxError: unexpected EOF while parsing SyntaxError:解析时出现意外的EOF

i try the same code in Jupyter notebook and it works fine 我在Jupyter笔记本中尝试了相同的代码,但效果很好

i tried this code ( i press enter after : then continue) 我尝试了此代码(我在输入:之后按Enter,然后继续)

for i in range(5):
    print(i)

or 要么

def printme( str ):
    "This prints a passed string into this function"
    print str
    return

the error point to the colon(:) in the first line in for loop or function definition 错误指向for循环或函数定义第一行中的冒号(:)

图片

图片

and as you can see, the same code is fine in Jupyter 如您所见,在Jupyter中可以使用相同的代码

图片

i want to know what is the problem in spyder, it works fine with other codes 我想知道spyder的问题是什么,它与其他代码兼容

This is not a spyder problem, but a formatting problem of your code. 这不是spyder问题,而是代码的格式问题。 A syntax error is raised, when there is an error in a line. 当一行中有错误时,将引发语法错误。 Not always does the indicated arrow point to the exact position of the error, it merely indicates where the interpreter stumbled. 并非总是所指示的箭头指向错误的确切位置,它仅指示解释器在哪里绊倒。 So the error most usually is somewhere near the indicated arrow. 因此,错误通常在指示箭头附近。

In your first case, there's nothing horribly wrong with the indicated line, so either you are doing something wrong in the next line (like missing indentation or something) or your interpreter doesn't allow using str as variable in the function definition, as it is a reserved keyword. 在第一种情况下,指示的行没有什么可怕的错误,因此,要么您在下一行中做错了什么(例如缺少缩进之类的东西),要么您的解释器不允许在函数定义中使用str作为变量,因为它是保留关键字。

In your second case, there is unallowed whitespace between the function name and the opening bracket. 在第二种情况下,函数名称和左括号之间不允许有空格。 This code should run both in jupyter and in spyder: 此代码应同时在jupyter和spyder中运行:

def printme(string):
  print(string)

for i in range(5):
  print(i)

Note: I added the brackets around the statement to print to be compatible with python 3. 注意:我在语句周围添加了括号,以进行print以与python 3兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM