473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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):

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
7 1653
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
**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
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
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
In article <11*********************@57g2000hsv.googlegroups.c om>,
Mark Peters <mp*******@gmail.comwrote:
Jul 2 '07 #6
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JC-Atl | last post by:
I am trying to do this in the SQL page of phpMyAdmin, but I get a syntax error. Can someone please help me out ? Thanks for your time and efforts! JC INSERT INTO `Test` ( `pkey` , `col2` , ...
6
by: Materialised | last post by:
Hi, could someone have a look at the following code and the error message below, and point out where my error is, as I am having trouble figuring where I am going wrong. #include <algorithm>...
13
by: Mtk | last post by:
Hi! Why does the following, simple, example produce such errors? I know it has to do with the two header files including each other and (moreover) the usage of the classes One and Two in the...
3
by: Catweasel | last post by:
I'm new to C++ and have been chucked in at the deep-end. I have a C++ console app that works fine. All I want to do is write to file however as soon as I include the fstream library and try to...
30
by: Brian Elmegaard | last post by:
Hi, I am struggling to understand how to really appreciate object orientation. I guess these are FAQ's but I have not been able to find the answers. Maybe my problem is that my style and...
11
by: Dagwood Bumstead | last post by:
I play around with js a little... I just don't get this. The file below is just trying out some things... it does exactly what I want (hides/displays some things, no big deal) The problem is...
15
by: gjoneshtfc | last post by:
Hello, I have a simple problem that I just cannot get my head around! I currently have the following line in my ASP recordset: Recordset1.Source = "SELECT * FROM MainTable ORDER BY Price ASC"...
5
by: Control Freq | last post by:
Hi I connect to my MySQL database using MyODBC. I have a table called 'log' in a database called 'home'. When I try to do an insert like this: insert into log (userid,event,event_date) values...
8
by: Bern McCarty | last post by:
I have a simple ref class in its own namespace that needs to coexist with a legacy typedef alias for "unsigned int" in the global namespace that has the identifier as itself. Everything compiles...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.