Re: Probably simple syntax error



On Jul 2, 2:40 pm, Dustin MacDonald <dmacdonal...@xxxxxxxxx> 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.

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.

Problem 2: excessive verbosity

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:

Try this:

weights_array = []
assert len(wordlist_both) == 36 # ???
for _unused in range(len(wordlist_both)):
# calculate something
weights_array.append(something)


big_randomized_int = RandRange(0,100)

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

# Make a random value and store it.
small_randomized_int = big_randomized_int / 100

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.

# Divide that random value and store it in a different variable.

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

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

**weights_array(randomizing_counter) = small_randomized_int
# Assign the first randomized value to our first word to be weighted.

First? It's done each time around the loop.

randomizing_counter = randomizing_counter + 1
# Up the counter and repeat.
[/code]


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

.



Relevant Pages

  • Re: making windows apps
    ... If you follow the newsgroup, ... they have to have the python interpreter installed? ... either don't mind having their users download Python (it's trivially ... generally use other tools such as InnoSetup to build nice installers. ...
    (comp.lang.python)
  • Probably simple syntax error
    ... This is my first time posting to this newsgroup, ... it the best I could into Python (ended up being one line shy of 80 ... # Divide that random value and store it in a different variable. ...
    (comp.lang.python)
  • Re: round() wrong in Python 2.4?
    ... >> I would say the usual floating point problem is involved. ... Now which number python should choose when it is ... Python didn't change anything between 2.3 and 2.4 wrt round(). ... > obviously binary floating point representations are involved. ...
    (comp.lang.python)
  • Re: web crawler in python or C?
    ... C or Python?Python though has fast development cycle but my concern is ... Python does not ... support multithreading, but it does support weak coroutines. ... The problem is that you've picked completely the wrong newsgroup to ask ...
    (comp.lang.c)
  • Re: is int(round(val)) safe?
    ... Tim Peters wrote: ... It's quite defensible to say that round returns an integer ... doesn't Python 2.4 have a speedy implementation of infinite-dimensional ... as a Python dict: ...
    (comp.lang.python)