By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,728 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
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 -
= 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
  1.     bet = []
  2.     for i in range(chromlen):                                   
  3.         if (random.uniform(0,1) <= 0.5):
  4.             bet.append((a-b*(math.log(random.uniform(0,1)+0.000000000001)))
  5.         else:
  6.             bet.append((a+b*(math.log(random.uniform(0,1)+0.000000000001)))
  7.     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
Share this Question
Share on Google+
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
  1.         if (random.uniform(0,1) <= 0.5):
  2.              bet.append((a-b*(math.log(random.uniform(0,1)+0.000000000001)))
  4. ## use instead
  5.         this_random = random.uniform(0,1)
  6.         if (this_random <= 0.5):
  7.              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
  1.         first = random.uniform(0,1)
  2.         print "testing %f for <= 0.5" % (first)
  3.         if first <= 0.5:
  4.              second = random.uniform(0,1)
  5.              print "appending %f" % (second)
  6.              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

Post your reply

Sign in to post your reply or Sign up for a free account.