1

Have been to trying to learn python from a couple of days, encountered a syntax error but seems to work in the tutorial that I am learning from, here's the code

def func(a):
    for i in range(a,10):
        print(i,end=' ')

func(2)

And the error

print(i,end=' ')
           ^

SyntaxError: invalid syntax

3
  • 2
    try python --version, are you positive that you are running at least version 3.0? I believe that syntax isn't permissible in earlier versions. Commented Feb 22, 2013 at 7:38
  • @eazar001 yes i am as the code works fine without the end=' ' Commented Feb 22, 2013 at 10:43
  • appears in the eclipse preferences was set to v2.7,thanks for the help! Commented Feb 22, 2013 at 11:05

1 Answer 1

3

In Python 3 this should work almost fine, however this will not work in Python 2 as it is a different syntax here is the code modified to work for different Python versions

Python 3

def func(a):
    for i in range(a,10):
        print(i,end=' ')
>>> func(1)
>>> 1 2 3 4 5 6 7 8 9

Python 2

def func(a):
    for i in range(a,10):
        print i, # Trailing comma to signify not to start a new line

>>> func(1)
>>> 1 2 3 4 5 6 7 8 9

Additional detail

https://docs.python.org/3/whatsnew/3.0.html#common-stumbling-blocks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.