Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error whenever i try to use sys.stderr

Tags:

python-3.x

I've got a problem using the standard error, whenever I try to use it my computer gives me a syntax error which i can't 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 this is my error

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 me why this happens, thanks in advance!

like image 921
Luuk Looijenga Avatar asked Dec 14 '14 17:12

Luuk Looijenga


1 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 can't be used like that, causing a syntax error.

You should look for some way to run Python 3.x instead.

For this particular case, you could also use from __future__ import print_function, which would make the code compatible with both versions.

like image 133
Oleh Prypin Avatar answered Nov 02 '22 00:11

Oleh Prypin