Invalid syntax error

Discussion in 'Python' started by sl33k, Mar 10, 2012.

  1. sl33k

    sl33k Guest

    I'm trying project euler problem 3 and I've hit the wall with this
    error. What could be the problem here?

    l=[]
    >>> num=600851475143
    >>> i=1
    >>> while i<=num:

    .... if num%i==0:
    .... l.append(i)
    .... i+=1
    .... print max(l)
    File "<stdin>", line 5
    print max(l)
    ^
    SyntaxError: invalid syntax
     
    sl33k, Mar 10, 2012
    #1
    1. Advertising

  2. sl33k

    Amit Sethi Guest

    Its an indentation error

    --
    A-M-I-T S|S
     
    Amit Sethi, Mar 10, 2012
    #2
    1. Advertising

  3. sl33k

    Andrew Berg Guest

    On 3/10/2012 6:34 AM, sl33k wrote:
    > I'm trying project euler problem 3 and I've hit the wall with this
    > error. What could be the problem here?
    >
    > l=[]
    >>>> num=600851475143
    >>>> i=1
    >>>> while i<=num:

    > ... if num%i==0:
    > ... l.append(i)
    > ... i+=1
    > ... print max(l)
    > File "<stdin>", line 5
    > print max(l)
    > ^
    > SyntaxError: invalid syntax
    >
    >

    You must be using Python 3. Along with many, many other changes, Python
    3 uses a print function instead of a print statement. If you want to
    follow along with the problems, use the version of Python it uses (2.7
    is probably safe if there isn't a version specified).

    --
    CPython 3.2.2 | Windows NT 6.1.7601.17640
     
    Andrew Berg, Mar 10, 2012
    #3
  4. 2012/3/10 sl33k <>:
    > I'm trying project euler problem 3 and I've hit the wall with this
    > error. What could be the problem here?
    >
    >  l=[]
    >>>> num=600851475143
    >>>> i=1
    >>>> while i<=num:

    > ...     if num%i==0:
    > ...         l.append(i)
    > ...     i+=1
    > ... print max(l)
    >  File "<stdin>", line 5
    >    print max(l)
    >        ^
    > SyntaxError: invalid syntax
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Hi,
    if you are using python 3, you'd (most likely) need to adapt the code
    written for python 2.
    see:
    http://docs.python.org/py3k/whatsnew/3.0.html#print-is-a-function

    hth,
    vbr
     
    Vlastimil Brom, Mar 10, 2012
    #4
  5. 在 2012å¹´3月10日星期六UTC+8下åˆ8æ—¶34分35秒,sl33k写é“:
    > I'm trying project euler problem 3 and I've hit the wall with this
    > error. What could be the problem here?
    >
    > l=[]
    > >>> num=600851475143
    > >>> i=1
    > >>> while i<=num:

    > ... if num%i==0:
    > ... l.append(i)
    > ... i+=1
    > ... print max(l)
    > File "<stdin>", line 5
    > print max(l)
    > ^
    > SyntaxError: invalid syntax


    It is a indentation error. It should be like:

    >>> while i<=num:

    .... if num%i==0:
    .... l.append(i)
    .... i+=1
    ....
    >>> print max(l)
     
    liuerfire Wang, Mar 10, 2012
    #5
  6. In article
    <>,
    sl33k <> wrote:

    >I'm trying project euler problem 3 and I've hit the wall with this
    >error. What could be the problem here?
    >
    > l=[]
    >>>> num=600851475143
    >>>> i=1
    >>>> while i<=num:

    >... if num%i==0:
    >... l.append(i)
    >... i+=1
    >... print max(l)
    > File "<stdin>", line 5
    > print max(l)
    > ^
    >SyntaxError: invalid syntax


    You have to insert an empty line after the end of the while loop (before
    the print command), so that the interpreter can run and finish the loop
    before it is to print the result.



    Best regards,

    Günther
     
    Günther Dietrich, Mar 10, 2012
    #6
  7. sl33k

    Ian Kelly Guest

    On Sat, Mar 10, 2012 at 6:17 AM, Günther Dietrich
    <> wrote:
    > In article
    > <>,
    >  sl33k <> wrote:
    >
    >>I'm trying project euler problem 3 and I've hit the wall with this
    >>error. What could be the problem here?
    >>
    >> l=[]
    >>>>> num=600851475143
    >>>>> i=1
    >>>>> while i<=num:

    >>...     if num%i==0:
    >>...         l.append(i)
    >>...     i+=1
    >>... print max(l)
    >>  File "<stdin>", line 5
    >>    print max(l)
    >>        ^
    >>SyntaxError: invalid syntax

    >
    > You have to insert an empty line after the end of the while loop (before
    > the print command), so that the interpreter can run and finish the loop
    > before it is to print the result.


    Note that this only applies to the interactive interpreter, to help it
    identify when to terminate the block and pass on to the compiler.
    When running a script, the extra blank lines are unnecessary, and
    indentation alone identifies the blocks.
     
    Ian Kelly, Mar 10, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFyayBQaGFuaWRlcw==?=

    Intermittant redirect to Invalid Syntax Error page

    =?Utf-8?B?TWFyayBQaGFuaWRlcw==?=, Feb 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    493
    =?Utf-8?B?TWFyayBQaGFuaWRlcw==?=
    Feb 16, 2004
  2. Patrick.O.Ige
    Replies:
    1
    Views:
    1,965
    Patrick.O.Ige
    Jul 2, 2006
  3. py
    Replies:
    2
    Views:
    414
  4. Tim Golden

    RE: WMI - invalid syntax error?

    Tim Golden, Dec 30, 2005, in forum: Python
    Replies:
    1
    Views:
    525
  5. Mark Richards
    Replies:
    3
    Views:
    315
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page