Probably simple syntax error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dustin  MacDonald

    Probably simple syntax error

    Hi everyone.

    This is my first time posting to this newsgroup, and although I
    maintain my netiquette I might've missed something specific to the
    newsgroup, so hopefully you can avoid flaming me if I have :) I
    apologize for the length of this post but I figure the more
    information the better.

    My problem is that I'm getting a syntax error in some Python code that
    looks quite simple. The original code was in Object Pascal as I'm a
    recent Delphi-turned-Python programmer.

    I took the code (which is only about 130 lines in OP) and 'translated'
    it the best I could into Python (ended up being one line shy of 80
    when I was done). I can't see any problems with the code but it's
    coming up with a bunch of errors, which I'm guessing are probably my
    assuming something is the same in Python as it is in Pascal, and being
    wrong.

    Anyway, here's the code I'm having trouble with (the same error comes
    up several times but this is the first part of the code it shows up
    in):

    Code:
    randomizing_counter = 0
    # Put the loop counter for the randomizing to zero.
    until_val = 36
    # Set the "until val" to 36. We'll compare them to make sure we're not
    at the end of our wordlist_both.
    
    while randomizing_counter < until_val:
    big_randomized_int = RandRange(0,100)
    # Make a random value and store it.
    small_randomized_int = big_randomized_int / 100
    # Divide that random value and store it in a different variable.
    small_randomized_int = Round(small_randomized_int, 2)
    # Round that value to 2 decimal places
    **weights_array(randomizing_counter) = small_randomized_int
    # Assign the first randomized value to our first word to be weighted.
    randomizing_counter = randomizing_counter + 1
    # Up the counter and repeat.
    The starred line is the one getting the error message: "SyntaxErro r:
    can't assign to function call"

    Now, I do understand what this means. I'm trying to assign to a
    function instead of the value that the function should create. But
    since when is weights_array or small_randomizi ng_int a function? Both
    are being declared in the code on their first usage. This one has got
    me stumped, maybe you guys can help me out with it?

    Thanks,
    ~Dustin

  • Gary Herron

    #2
    Re: Probably simple syntax error

    Dustin MacDonald wrote:
    Hi everyone.
    >
    This is my first time posting to this newsgroup, and although I
    maintain my netiquette I might've missed something specific to the
    newsgroup, so hopefully you can avoid flaming me if I have :) I
    apologize for the length of this post but I figure the more
    information the better.
    >
    My problem is that I'm getting a syntax error in some Python code that
    looks quite simple. The original code was in Object Pascal as I'm a
    recent Delphi-turned-Python programmer.
    >
    I took the code (which is only about 130 lines in OP) and 'translated'
    it the best I could into Python (ended up being one line shy of 80
    when I was done). I can't see any problems with the code but it's
    coming up with a bunch of errors, which I'm guessing are probably my
    assuming something is the same in Python as it is in Pascal, and being
    wrong.
    >
    Anyway, here's the code I'm having trouble with (the same error comes
    up several times but this is the first part of the code it shows up
    in):
    >
    Code:
    randomizing_counter = 0
    # Put the loop counter for the randomizing to zero.
    until_val = 36
    # Set the "until val" to 36. We'll compare them to make sure we're not
    at the end of our wordlist_both.
    >
    while randomizing_counter < until_val:
    	big_randomized_int = RandRange(0,100)
    	# Make a random value and store it.
    	small_randomized_int = big_randomized_int / 100
    	# Divide that random value and store it in a different variable.
    	small_randomized_int = Round(small_randomized_int, 2)
    	# Round that value to 2 decimal places
    	**weights_array(randomizing_counter) = small_randomized_int
    >
    Code:
    If  weights_array is a list or dictionary then use
    weights_array[randomizing_counter] = ...
    to choose which element of the list gets the assignment.
    
    If weights_array is a function, then
    weights_array(randomizing_counter)
    is the proper way to call the function, but the value returned cannot be
    the object of an assignment.
    
    Gary Herron
    [QUOTE]
    	# Assign the first randomized value to our first word to be weighted.
    	randomizing_counter = randomizing_counter + 1
    	# Up the counter and repeat.
    [/QUOTE]
    >
    The starred line is the one getting the error message: "SyntaxErro r:
    can't assign to function call"
    >
    Now, I do understand what this means. I'm trying to assign to a
    function instead of the value that the function should create. But
    since when is weights_array or small_randomizi ng_int a function? Both
    are being declared in the code on their first usage. This one has got
    me stumped, maybe you guys can help me out with it?
    >
    Thanks,
    ~Dustin
    >
    >

    Comment

    • Mark Peters

      #3
      Re: Probably simple syntax error

      **weights_array (randomizing_co unter) = small_randomize d_int
      >
      The starred line is the one getting the error message: "SyntaxErro r:
      can't assign to function call"
      >
      Now, I do understand what this means. I'm trying to assign to a
      function instead of the value that the function should create. But
      since when is weights_array or small_randomizi ng_int a function? Both
      are being declared in the code on their first usage. This one has got
      me stumped, maybe you guys can help me out with it?
      Using parens () indicates a function call.

      I suspect things will work much better using square brackets [].

      Square brackets indicate the index into a sequence (like a list)

      Comment

      • John Machin

        #4
        Re: Probably simple syntax error

        On Jul 2, 2:40 pm, Dustin MacDonald <dmacdonal...@g mail.comwrote:
        Hi everyone.
        >
        This is my first time posting to this newsgroup, and although I
        maintain my netiquette I might've missed something specific to the
        newsgroup, so hopefully you can avoid flaming me if I have :) I
        apologize for the length of this post but I figure the more
        information the better.
        The more relevant information the better. It would have helped had you
        shown (a) the first use of weights_array (b) your import statements
        >
        My problem is that I'm getting a syntax error in some Python code that
        looks quite simple. The original code was in Object Pascal as I'm a
        recent Delphi-turned-Python programmer.
        >
        I took the code (which is only about 130 lines in OP) and 'translated'
        it the best I could into Python (ended up being one line shy of 80
        when I was done). I can't see any problems with the code but it's
        coming up with a bunch of errors, which I'm guessing are probably my
        assuming something is the same in Python as it is in Pascal, and being
        wrong.
        >
        Anyway, here's the code I'm having trouble with (the same error comes
        up several times but this is the first part of the code it shows up
        in):
        Others have pointed out that the likely source of your first problem
        is using () instead of [] for list subscripting.

        I'll move on to the next few ...
        >
        Code:
        randomizing_counter = 0
        # Put the loop counter for the randomizing to zero.
        Code:
        Problem 2: excessive verbosity
        [QUOTE]
        until_val = 36
        # Set the "until val" to 36. We'll compare them to make sure we're not
        at the end of our wordlist_both.
        >
        while randomizing_counter < until_val:[/QUOTE]
        
        Try this:
        
        weights_array = []
        assert len(wordlist_both) == 36 # ???
        for _unused in range(len(wordlist_both)):
        # calculate something
        weights_array.append(something)
        
        [QUOTE]
                big_randomized_int = RandRange(0,100)[/QUOTE]
        
        Problem 3a:
        You have an import statement like
        import random
        in which case you would get a runtime error, and should have:
        .... = random.randrange(0, 100)
        or Problem 3b:
        You have an import statement like:
        from random import randrange as RandRange
        which will not cause a runtime error, merely mass barfing among the
        spectators :-)
        [QUOTE]
                # Make a random value and store it.
                small_randomized_int = big_randomized_int / 100[/QUOTE]
        
        Problem 5: putting comments after the code instead of on the same line
        as the statement you think needs explanation (most don't) or before
        it.
        [QUOTE]
                # Divide that random value and store it in a different variable.[/QUOTE]
        
        Problem 6: big_randomized_int can only have values in 0, 1, ..., 98,
        99. So small_randomized_int will have the value 0, always.
        
        Perhaps you meant:
        small_randomised_float = big_randomized_int / 100.0
        [QUOTE]
                small_randomized_int = Round(small_randomized_int, 2)
                # Round that value to 2 decimal places[/QUOTE]
        
        Problem 7: even if you did intend big.... / 100.00, the above is
        redundant. 1 / 100.0 is 0.01, 99 / 100.0 is 0.99 -- no rounding is
        necessary.
        
        Problem 8: it's round(), not Round()
        [QUOTE]
                **weights_array(randomizing_counter) = small_randomized_int
                # Assign the first randomized value to our first word to be weighted.[/QUOTE]
        
        First? It's done each time around the loop.
        [QUOTE]
                randomizing_counter = randomizing_counter + 1
                # Up the counter and repeat.
        [/QUOTE]
        >
        So, here's the looping version:

        weights_array = []
        for _unused in range(len(wordl ist_both)):
        weights_array.a ppend(random.ra ndrange(100) / 100.0)

        and here's the obligatory one-liner, using a list comprehension:

        weights_array = [random.randrang e(100) / 100.0 for _unused in
        range(len(wordl ist_both))]

        and here's an example of it in use:
        >>import random
        >>wordlist_bo th = 10 * ['foo']
        >>weights_arr ay = [random.randrang e(100) / 100.0 for _unused in range(len(wordl ist_both))]
        >>weights_arr ay
        [0.38, 0.12, 0.5500000000000 0004, 0.2399999999999 9999,
        0.9100000000000 0003, 0.4899999999999 9999, 0.9100000000000 0003,
        0.6700000000000 0004, 0.7700000000000 0002,
        0.8199999999999 9995]
        >>>
        Problem 9: you were expecting "precise" values like 0.55 and 0.24.

        Solution is to read this:


        HTH,
        John

        Comment

        • ptn

          #5
          Re: Probably simple syntax error

          >
          Problem 6: big_randomized_ int can only have values in 0, 1, ..., 98,
          99. So small_randomize d_int will have the value 0, always.
          >
          Perhaps you meant:
          small_randomise d_float = big_randomized_ int / 100.0
          >
          small_randomize d_int = Round(small_ran domized_int, 2)
          # Round that value to 2 decimal places
          >
          PASCAL -- PYTHON
          5 div 2 -- 5/2
          5 mod 2 -- 5 % 2
          5/2 -- 5/2. (Notice the little dot at the end)

          Comment

          • Cameron Laird

            #6
            Re: Probably simple syntax error

            In article <1183352843.803 263.82140@57g20 00hsv.googlegro ups.com>,
            Mark Peters <mpeters42@gmai l.comwrote:

            Comment

            • Dustin  MacDonald

              #7
              Re: Probably simple syntax error

              Ah. Thank you everyone. Sorry for not replying earlier, real life got
              in the way :)

              Gerry Herron, Tim Delaney, Mark Peters: Thank you. Switching from
              parentheses to square brackets fixed the code, and yes, Tim, you were
              right. It was a list I was working with. And thanks for those links
              Tim.

              John Machin: Thank you for all the pointers/code fixes there. They'll
              help alot.

              Ptn: I was unaware of that period added, Thanks, I'll have to watch
              out for it. :)

              And Cameron: Ah, yes. It does reduce the confusion. I do know that
              square brackets are used for *creating* a dictionary (blah = ["A",
              "B", "C"], so I figured the same would apply to accessing it (which is
              why for my list, which I created with parenthesis I assumed I accessed
              with parenthesis). Thank you =]

              ~Dustin

              Comment

              • Duncan Booth

                #8
                Re: Probably simple syntax error

                ptn <tn.pablo@gmail .comwrote:
                PASCAL -- PYTHON
                5 div 2 -- 5/2
                better: 5//2

                The behaviour of 5/2 varies according to command line options and/or
                __future__ imports. e.g. If you start Python with the -Qwarn option 5/2
                will generate a warning; if you start Python with -Qnew (or use "from
                __future__ import division") then 5/2 will give you 2.5.

                It is best, if you mean integer division, to always use the integer
                division operator.
                5 mod 2 -- 5 % 2
                5/2 -- 5/2. (Notice the little dot at the end)
                Also note that those relationships only hold where both operands are
                positive.

                Python: -3//2 ----2
                Pascal: -3 div 2 ---either -1 or -2 is allowed by the standard.

                Python: 3 % -2 ----1
                Pascal: 3 mod -2 --error

                Comment

                Working...