Probably simple syntax error

  • Thread starter Dustin MacDonald
  • Start date
D

Dustin MacDonald

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: "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
 
G

Gary Herron

Dustin said:
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
[/QUOTE]
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.

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
 
M

Mark Peters

**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)
 
J

John Machin

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.[/QUOTE]

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.

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
 
P

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

PASCAL --> PYTHON
5 div 2 --> 5/2
5 mod 2 --> 5 % 2
5/2 --> 5/2. (Notice the little dot at the end)
 
C

Cameron Laird

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

I'm wary of this line, especially in isolation. I hope it reduces,
rather than exacerbates, the original questioner's confusion, for me
to observe that [] *also* mark dereferencing into a dictionary (hash,
associative array, ...), which is distinct from list indexing.
 
D

Dustin MacDonald

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
 
J

John Machin

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"],

That's a list, not a dictionary.
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).

If you created it with parentheses, it's not a list, it's a tuple.
Problem 10: Tuples are immutable.
atuple[index_variable] = something
and
atuple.append(something)
will fail.

Print this, cut it out, and paste it inside your hat:
"""
alist = ['foo', 42, True, 123.456]
assert alist[1] == 42

atuple = ('foo', 42, True, 123.456)
assert atuple[1] == 42

adict = {'bar': 'foo', 1: 42, 'abool': True, 'afloat': 123.456}
assert adict[1] == 42
"""

and, in general:

Abjure guesswork, read the documentation!
 
D

Duncan Booth

ptn said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top