syntax error in sum(). Please explicate.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Press

    #1

    syntax error in sum(). Please explicate.

    I have not written python codes nor run any. I saw this
    code posted and decided to try it. It fails. I read the
    tutorial and the entry for the built in function sum,
    but still do not see the problem. The code was cut and
    paste. Please help. Thanks.

    _______________ __________BEGIN _CODE__________ _______________
    #!/usr/bin/python

    ps = [none, 2,3,5,7,11,13,1 7,19,23,29]

    def phi(x, a):
    return x - sum(phi(x // ps[i+1], i) for i in range(a))

    def pi(n):
    from math import sqrt
    if n <= 1:
    return 0
    a = pi(int(sqrt(n)) )
    return phi(n, a) + a - 1
    _______________ ___________END_ CODE___________ _______________

    Here is the result of running it:

    File "/Users/mdp/source/prime_counter_p ython", line 6
    return x - sum(phi(x // ps[i+1], i) for i in range(a))
    ^
    SyntaxError: invalid syntax

    Here are some lines from python -v:

    Python 2.3 (#1, Sep 13 2003, 00:49:11)
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
    Type "help", "copyright" , "credits" or "license" for more information

    --
    Michael Press
  • tom

    #2
    Re: syntax error in sum(). Please explicate.

    Michael Press wrote:
    I have not written python codes nor run any. I saw this
    code posted and decided to try it. It fails. I read the
    tutorial and the entry for the built in function sum,
    but still do not see the problem. The code was cut and
    paste. Please help. Thanks.
    >
    _______________ __________BEGIN _CODE__________ _______________
    #!/usr/bin/python
    >
    ps = [none, 2,3,5,7,11,13,1 7,19,23,29]
    >
    >
    None has a capital N ^
    def phi(x, a):
    return x - sum(phi(x // ps[i+1], i) for i in range(a))
    >
    def pi(n):
    from math import sqrt
    if n <= 1:
    return 0
    a = pi(int(sqrt(n)) )
    return phi(n, a) + a - 1
    _______________ ___________END_ CODE___________ _______________
    >
    Here is the result of running it:
    >
    File "/Users/mdp/source/prime_counter_p ython", line 6
    return x - sum(phi(x // ps[i+1], i) for i in range(a))
    ^
    SyntaxError: invalid syntax
    >
    >
    can you paste the complete code? I'm not sure why that's a syntax error
    there. with a bit of fudging it seems to run in the interpreter ok...
    Here are some lines from python -v:
    >
    Python 2.3 (#1, Sep 13 2003, 00:49:11)
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
    Type "help", "copyright" , "credits" or "license" for more information
    >
    >

    Comment

    • Matt Moriarity

      #3
      Re: syntax error in sum(). Please explicate.

      try surrounding your sum argument in brackets:

      sum([phi(x // ps[i+1], i) for i in range(a)])

      instead of:

      sum(phi(x // ps[i+1], i) for i in range(a))

      On Nov 18, 5:23 pm, Michael Press <j...@abc.netwr ote:
      I have not written python codes nor run any. I saw this
      code posted and decided to try it. It fails. I read the
      tutorial and the entry for the built in function sum,
      but still do not see the problem. The code was cut and
      paste. Please help. Thanks.
      >
      _______________ __________BEGIN _CODE__________ _______________
      #!/usr/bin/python
      >
      ps = [none, 2,3,5,7,11,13,1 7,19,23,29]
      >
      def phi(x, a):
      return x - sum(phi(x // ps[i+1], i) for i in range(a))
      >
      def pi(n):
      from math import sqrt
      if n <= 1:
      return 0
      a = pi(int(sqrt(n)) )
      return phi(n, a) + a - 1
      _______________ ___________END_ CODE___________ _______________
      >
      Here is the result of running it:
      >
      File "/Users/mdp/source/prime_counter_p ython", line 6
      return x - sum(phi(x // ps[i+1], i) for i in range(a))
      ^
      SyntaxError: invalid syntax
      >
      Here are some lines from python -v:
      >
      Python 2.3 (#1, Sep 13 2003, 00:49:11)
      [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
      Type "help", "copyright" , "credits" or "license" for more information
      >
      --
      Michael Press

      Comment

      • Paul Rubin

        #4
        Re: syntax error in sum(). Please explicate.

        Michael Press <jack@abc.netwr ites:
        File "/Users/mdp/source/prime_counter_p ython", line 6
        return x - sum(phi(x // ps[i+1], i) for i in range(a))
        ^
        SyntaxError: invalid syntax
        >
        Here are some lines from python -v:
        >
        Python 2.3 (#1, Sep 13 2003, 00:49:11)
        Generator comprehensions like you used weren't introduced til
        Python 2.4, so 2.3 will raise a syntax error like you got.

        Comment

        • John Machin

          #5
          Re: syntax error in sum(). Please explicate.


          Michael Press wrote:
          I have not written python codes nor run any. I saw this
          code posted and decided to try it. It fails. I read the
          tutorial and the entry for the built in function sum,
          but still do not see the problem. The code was cut and
          paste.
          I doubt it -- "none" should be "None"

          Please help. Thanks.
          >
          _______________ __________BEGIN _CODE__________ _______________
          #!/usr/bin/python
          >
          ps = [none, 2,3,5,7,11,13,1 7,19,23,29]
          >
          def phi(x, a):
          return x - sum(phi(x // ps[i+1], i) for i in range(a))
          >
          def pi(n):
          from math import sqrt
          if n <= 1:
          return 0
          a = pi(int(sqrt(n)) )
          return phi(n, a) + a - 1
          _______________ ___________END_ CODE___________ _______________
          >
          Here is the result of running it:
          >
          File "/Users/mdp/source/prime_counter_p ython", line 6
          return x - sum(phi(x // ps[i+1], i) for i in range(a))
          ^
          SyntaxError: invalid syntax
          >
          Here are some lines from python -v:
          >
          Python 2.3 (#1, Sep 13 2003, 00:49:11)
          [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
          Type "help", "copyright" , "credits" or "license" for more information
          New syntax. Python 2.3 is *two* versions behind the current production
          version. Either (a) run it on Python 2.4 or 2.5 or (b) enclose the
          argument to sum in square brackets i.e.

          sum([blah blah blah]) instead of sum(blah blah blah)

          Cheers,
          John

          Comment

          • Michael Press

            #6
            Re: syntax error in sum(). Please explicate.

            In article
            <1163889196.135 008.126970@h54g 2000cwb.googleg roups.com>
            ,
            "Matt Moriarity" <matt.moriarity @gmail.comwrote :
            try surrounding your sum argument in brackets:
            >
            sum([phi(x // ps[i+1], i) for i in range(a)])
            >
            instead of:
            >
            sum(phi(x // ps[i+1], i) for i in range(a))
            Thank you. That makes it work.

            --
            Michael Press

            Comment

            • Tim Peters

              #7
              Re: syntax error in sum(). Please explicate.

              [Matt Moriarity]
              >try surrounding your sum argument in brackets:
              >>
              >sum([phi(x // ps[i+1], i) for i in range(a)])
              >>
              >instead of:
              >>
              >sum(phi(x // ps[i+1], i) for i in range(a))
              [Michael Press]
              Thank you. That makes it work.
              But is a wrong solution ;-) As others have suggested, it's almost
              certainly the case that you were using a too-old version of Python.
              2.5 is current, and 2.4.4 (the current bugfix release in the 2.4 line)
              would also work. You must have been using a version before 2.4, and
              if you're just starting with Python, it's a Bad Idea to artificially
              burden yourself with an old version where current examples can't work
              ;-)

              Comment

              • Bjoern Schliessmann

                #8
                Re: syntax error in sum(). Please explicate.

                Paul Rubin wrote:
                Generator comprehensions
                Are generator comprehensions and generator expressions the same?

                Regards,


                Björn

                --
                BOFH excuse #35:

                working as designed

                Comment

                • Fredrik Lundh

                  #9
                  Re: syntax error in sum(). Please explicate.

                  Bjoern Schliessmann wrote:
                  Are generator comprehensions and generator expressions the same?
                  the documentation uses the word "expression ", not comprehension.

                  </F>

                  Comment

                  Working...