By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

Probably simple syntax error

P: n/a
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):

Expand|Select|Wrap|Line Numbers
  1. randomizing_counter = 0
  2. # Put the loop counter for the randomizing to zero.
  3. until_val = 36
  4. # Set the "until val" to 36. We'll compare them to make sure we're not
  5. at the end of our wordlist_both.
  6.  
  7. while randomizing_counter < until_val:
  8. big_randomized_int = RandRange(0,100)
  9. # Make a random value and store it.
  10. small_randomized_int = big_randomized_int / 100
  11. # Divide that random value and store it in a different variable.
  12. small_randomized_int = Round(small_randomized_int, 2)
  13. # Round that value to 2 decimal places
  14. **weights_array(randomizing_counter) = small_randomized_int
  15. # Assign the first randomized value to our first word to be weighted.
  16. randomizing_counter = randomizing_counter + 1
  17. # Up the counter and repeat.
  18.  
The starred line is the one getting the error message: "SyntaxError:
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_randomizing_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

Jul 2 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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):

Expand|Select|Wrap|Line Numbers
  1. randomizing_counter = 0
  2. # Put the loop counter for the randomizing to zero.
  3. until_val = 36
  4. # Set the "until val" to 36. We'll compare them to make sure we're not
  5. at the end of our wordlist_both.
  6. while randomizing_counter < until_val:
  7.     big_randomized_int = RandRange(0,100)
  8.     # Make a random value and store it.
  9.     small_randomized_int = big_randomized_int / 100
  10.     # Divide that random value and store it in a different variable.
  11.     small_randomized_int = Round(small_randomized_int, 2)
  12.     # Round that value to 2 decimal places
  13.     **weights_array(randomizing_counter) = small_randomized_int
Expand|Select|Wrap|Line Numbers
  1. If  weights_array is a list or dictionary then use
  2. weights_array[randomizing_counter] = ...
  3. to choose which element of the list gets the assignment.
  4.  
  5. If weights_array is a function, then
  6. weights_array(randomizing_counter)
  7. is the proper way to call the function, but the value returned cannot be
  8. the object of an assignment.
  9.  
  10. Gary Herron
  11.  
  12.         
  13.                     # Assign the first randomized value to our first word to be weighted.
  14.     randomizing_counter = randomizing_counter + 1
  15.     # Up the counter and repeat.
  16.  
  17.  
>
The starred line is the one getting the error message: "SyntaxError:
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_randomizing_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

Jul 2 '07 #2

P: n/a
**weights_array(randomizing_counter) = small_randomized_int
>
The starred line is the one getting the error message: "SyntaxError:
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_randomizing_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)

Jul 2 '07 #3

P: n/a
On Jul 2, 2:40 pm, Dustin MacDonald <dmacdonal...@gmail.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 ...
>
Expand|Select|Wrap|Line Numbers
  1. randomizing_counter = 0
  2. # Put the loop counter for the randomizing to zero.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Problem 2: excessive verbosity
  3.  
  4.         
  5.                 until_val = 36
  6. # Set the "until val" to 36. We'll compare them to make sure we're not
  7. at the end of our wordlist_both.
  8. while randomizing_counter < until_val:
  9.  
  10. Try this:
  11.  
  12. weights_array = []
  13. assert len(wordlist_both) == 36 # ???
  14. for _unused in range(len(wordlist_both)):
  15. # calculate something
  16. weights_array.append(something)
  17.  
  18.  
  19.         
  20.                         big_randomized_int = RandRange(0,100)
  21.  
  22. Problem 3a:
  23. You have an import statement like
  24. import random
  25. in which case you would get a runtime error, and should have:
  26. .... = random.randrange(0, 100)
  27. or Problem 3b:
  28. You have an import statement like:
  29. from random import randrange as RandRange
  30. which will not cause a runtime error, merely mass barfing among the
  31. spectators :-)
  32.  
  33.         
  34.                         # Make a random value and store it.
  35.         small_randomized_int = big_randomized_int / 100
  36.  
  37. Problem 5: putting comments after the code instead of on the same line
  38. as the statement you think needs explanation (most don't) or before
  39. it.
  40.  
  41.         
  42.                         # Divide that random value and store it in a different variable.
  43.  
  44. Problem 6: big_randomized_int can only have values in 0, 1, ..., 98,
  45. 99. So small_randomized_int will have the value 0, always.
  46.  
  47. Perhaps you meant:
  48. small_randomised_float = big_randomized_int / 100.0
  49.  
  50.         
  51.                         small_randomized_int = Round(small_randomized_int, 2)
  52.         # Round that value to 2 decimal places
  53.  
  54. Problem 7: even if you did intend big.... / 100.00, the above is
  55. redundant. 1 / 100.0 is 0.01, 99 / 100.0 is 0.99 -- no rounding is
  56. necessary.
  57.  
  58. Problem 8: it's round(), not Round()
  59.  
  60.         
  61.                         **weights_array(randomizing_counter) = small_randomized_int
  62.         # Assign the first randomized value to our first word to be weighted.
  63.  
  64. First? It's done each time around the loop.
  65.  
  66.         
  67.                         randomizing_counter = randomizing_counter + 1
  68.         # Up the counter and repeat.
  69.  
  70.  
>
So, here's the looping version:

weights_array = []
for _unused in range(len(wordlist_both)):
weights_array.append(random.randrange(100) / 100.0)

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

weights_array = [random.randrange(100) / 100.0 for _unused in
range(len(wordlist_both))]

and here's an example of it in use:
>>import random
wordlist_both = 10 * ['foo']
weights_array = [random.randrange(100) / 100.0 for _unused in range(len(wordlist_both))]
weights_array
[0.38, 0.12, 0.55000000000000004, 0.23999999999999999,
0.91000000000000003, 0.48999999999999999, 0.91000000000000003,
0.67000000000000004, 0.77000000000000002,
0.81999999999999995]
>>>
Problem 9: you were expecting "precise" values like 0.55 and 0.24.

Solution is to read this:
http://docs.python.org/tut/node16.html

HTH,
John

Jul 2 '07 #4

P: n/a
ptn
>
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
small_randomized_int = Round(small_randomized_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)

Jul 2 '07 #5

P: n/a
In article <11*********************@57g2000hsv.googlegroups.c om>,
Mark Peters <mp*******@gmail.comwrote:
Jul 2 '07 #6

P: n/a
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

Jul 2 '07 #7

P: n/a
ptn <tn******@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
Jul 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.