简体   繁体   中英

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

SyntaxError: unexpected EOF while parsing

i try the same code in Jupyter notebook and it works fine

i tried this code ( i press enter after : then continue)

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

图片

图片

and as you can see, the same code is fine in Jupyter

图片

i want to know what is the problem in spyder, it works fine with other codes

This is not a spyder problem, but a formatting problem of your code. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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