473,406 Members | 2,956 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,406 software developers and data experts.

Invalid Syntax in else statement

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
  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
  8.  
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 4880
dwblas
626 Expert 512MB
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)))
  3.  
  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
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
dwblas
626 Expert 512MB
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
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

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

Similar topics

4
by: Bradley Kite | last post by:
Hi all. I'm trying to diagnose/solve a problem with internet explorer, whereby sometimes the form submits, and other times IE produces an 'invalid syntax' error. First, I have a form, and...
5
by: DIBS | last post by:
I'm new to Python and I don't understand what I'm doing wrong. I'm running windows xp. In the command line window, I type: Python Sudoku.py and I get the response" SyntaxError: invalid...
1
by: akcharter | last post by:
I am incorporating a pre-existing classifieds program into my website. I would like to charge a nominal fee for ads listing items over $100. I think an if ... else statement would work best in this...
10
by: ronrsr | last post by:
no matter where I place this imported file,the statement after it in the main program gets a syntax error, regardless of the syntax. I think I may have changed something in this file, but I'm...
16
by: bitong | last post by:
Just cant figure out how to formulate the correct syntax..I can do it in looping statement but my professor said that use only the simple if-else statement. The problem goes like this..Write a...
7
by: alf | last post by:
Hi, I wonder why it is an invalid syntax: File "<stdin>", line 1 if 1: if 1: if 1: print 1 or
6
by: Nathan Pinno | last post by:
Why does my compiler say invalid syntax and then highlight the quotation marks in the following code: # This program is to find primes. primes = import math import gmpy while 1: run =...
1
by: Karen Rosen | last post by:
This is the first time I am attempting this - someone was kind enough to help me with my previous error and told me to add print statements before the error which I did the error is now invalid...
1
by: Richard Bryant | last post by:
I`m new to python and I do`t understand why it repond:invalid syntax when I run this: x=12 y=7 z=8 if x<y and y<z: print ('z is the largest') elif x<y and y>z: print ('y is the largest') else...
5
by: Clabbage | last post by:
For some odd reason my code was all working fine until I made a few changes which broke almost everything in the function. I've since removed said changes but it's still broken. It's saying the colon...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.