On Jul 2, 2:40 pm, Dustin MacDonald <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list