Syntax error whenever I try to use sys.stderr

I have a standard error problem, when I try to use it, my computer gives me a syntax error that I cannot explain.

So this is my code:

import sys

def main(argv):
if len(argv) != 3:
    print("Usage: python walk.py n l", file=sys.stderr)
else:
    l = argv[2]
    n = argv[1]
    print("You ended up", simuleer(n,l), "positions from the starting point.")



if __name__ == "__main__":
main(sys.argv)

      

And that's my fault

MacBook-Air-van-Luuk:documents luuk$ python walk.py 5 1 2
File "walk.py", line 21
print("Usage: python walk.py n l", file=sys.stderr)    
                                       ^

      

I hope someone can explain to me why this is happening, thanks in advance!

+3


source to share


2 answers


You think you're using Python 3.x, but it's actually Python 2.x. On most systems, python

executable means Python 2.x.

print

is not a function in Python 2.x and cannot be used this way, causing a syntax error.



You should be looking for a way to run Python 3.x.

In this particular case, you can also use , which will make the code compatible with both versions.from __future__ import print_function

+8


source


There is a way to fix it

Just remove "file =" from the print method



eg:

print ("Usage: python walk.py n l", sys.stderr)

-2


source







All Articles