464,728 Members | 1,134 Online Need help? Post your question and get tips & solutions from a community of 464,728 IT Pros & Developers. It's quick & easy.

# Invalid Syntax in else statement

 P: 3 As part of a real coded genetic algorithm, I have to generate a random number beta that follows Laplace Distribution. The give relationship is - beta(i) = a-b*log(u(i)) [if r(i) <= 0.5] = a+b*log(u(i)) [if r(i) > 0.5] Where i varies from 1 to n (n is the length of the chromosome). r(i) and u(i) are uniform random numbers belonging to the interval [0,1]. a and b are constants. I want to make a list of beta values for each i and hence wrote the following lines of code: Expand|Select|Wrap|Line Numbers     bet = []     for i in range(chromlen):                                            if (random.uniform(0,1) <= 0.5):             bet.append((a-b*(math.log(random.uniform(0,1)+0.000000000001)))         else:             bet.append((a+b*(math.log(random.uniform(0,1)+0.000000000001)))     beta = bet   This way I have a list of beta values that have been generated randomly and follow Laplace Distribution I'm receiving a message when I run the program that says "There's an error in your program: invalid syntax" and it highlights 'else' in red. I can't figure out where I made a mistake. Please help. Thanks. I've imported 'random' and 'math' earlier in the code. As I mentioned before, this is just part of the code. Dec 16 '13 #1
4 Replies

 Expert 100+ P: 626 You are missing a closing parens, ), on the previous line. Also, you are using two different random numbers here and the second one does not have to be < 0.5. Expand|Select|Wrap|Line Numbers         if (random.uniform(0,1) <= 0.5):              bet.append((a-b*(math.log(random.uniform(0,1)+0.000000000001)))   ## use instead         this_random = random.uniform(0,1)         if (this_random <= 0.5):              bet.append((a-b*(math.log(this_random+0.000000000001))))    ## parens added  Dec 16 '13 #2

 P: 3 Thank you for your reply! The 2 numbers are supposed to be different as can be noticed from the relation I mentioned above. However, the parenthesis bit helped :) Dec 16 '13 #3

 Expert 100+ P: 626 Then the if/else statements make no sense as you append a new/different/second value whether the first random was < 0.5 or not. See the code example above to use the original, tested random to append to the list. Expand|Select|Wrap|Line Numbers         first = random.uniform(0,1)         print "testing %f for <= 0.5" % (first)         if first <= 0.5:              second = random.uniform(0,1)              print "appending %f" % (second)              bet.append((a-b*(math.log(second+0.000000000001)))  Dec 16 '13 #4

 P: 3 Hey, I'm sorry if there's any confusion been created from my side but the if and else statement is required because different expressions for beta(i) are obtained depending on the value of 'first'. beta(i) = a - b*log(u(i)) if first <= 0.5 beta(i) = a + b*log(u(i)) if first > 0.5 I understand that 'second' comes into play only when the conditions concerning first are satisfied. Dec 16 '13 #5 