0

Please find the below code block in python 2.7.

for i in range(len(p_routes)): if len(p_routes[i]) > 2 : if p_routes[i][2] == 'No Backup': K = K + 1 for z in range(len(p_routes[i])): nbup.write(K + 1 , z , p_routes[i][z]) elif p_routes[i][0][0] == 'E' : L = L + 1 for z in range(len(p_routes[i])): ex.write(L, z , (p_routes[i][z]) elif p_routes[i][0][0] == 'G': M = M + 1 for z in range(len(p_routes[i])) gh.write(M ,z, p_routes[i][z]) else len(p_routes[i]) < 2: pass print "\nFor some reason. " 

Well, I am getting an syntax error saying elif p_routes[i][0][0] == 'G': . I couldn't figure out why this error is coming as I believe there is no syntax error in this line.

The ex and gh are two excel sheet variable created before this code block. And p_routes is a list of list of 2 degrees. The format is like p_routes = [['prov1' , 'address1' , 'No Backup'] , ['prov2', 'address2', 'Back1', 'Back2' ]]

You might have understood, that the inner list length is a variable size. Any advise would be much appreciated. Sorry for the silly question but I did a lot of searching and re-formatting my if..else block in number of ways. But every time I am getting this error.

By the way previously the syntax error was with L = L + 1. Funny! Then I changed the type of L by L = int(L). Now, that error is gone.

3
  • 6
    You will be getting an error on the else statement as well, else doesn't have a condition, it's what is done if all other conditions are false. Besides, both pass and a print statement? Commented Sep 16, 2015 at 5:28
  • 3
    It is syntax error ex.write(L, z , (p_routes[i][z]) should be this ex.write(L, z , (p_routes[i][z])) and there are many other errors Commented Sep 16, 2015 at 5:28
  • Thanks a lot mate for your quick and to the point answer. As you pointed out, yes this was the issue indeed. Commented Sep 18, 2015 at 5:54

3 Answers 3

1

Notes:

Never forget to close the ( with )

Else will execute if none of the above case condition was right so you should not give any condition to else statement

Don't forget : in if else for.....

Changes to your code:

for i in range(len(p_routes)): if len(p_routes[i]) > 2 : if p_routes[i][2] == 'No Backup': K = K + 1 for z in range(len(p_routes[i])): nbup.write(K + 1 , z , p_routes[i][z]) elif p_routes[i][0][0] == 'E' : L = L + 1 for z in range(len(p_routes[i])): ex.write(L, z , (p_routes[i][z])) elif p_routes[i][0][0] == 'G': M = M + 1 for z in range(len(p_routes[i])): gh.write(M ,z, p_routes[i][z]) else : pass print "\nFor some reason. " 
Sign up to request clarification or add additional context in comments.

Comments

1

First off as Vignesh pointed out, your error is actually on the previous line as you forgot to close your parenthesis ( )

Second, the else clause for the if, elif, else structure does not require a check. Here is a video I made a while ago with how selection works in python linked to relevant time

(May not be relevant) Also keep in mind with your current logic, what happens if: len(p_routes[i]) is 2? you currently only check if it's less than two or greater than 2.

1 Comment

Thanks mate for pointing out the possible bug. But it was kept intentionally as the variable mentioned here would be a output from some ssh commands and as the way I did the regex filtering, the possible outcome could only be <2 or >2.
0

With syntax errors, it is always wise to have a look at the preceding line to make sure it is also correct. As you missed the closing ), Python kept looking for it on the next line.

There are a number of areas you could make the code a bit cleaner. For example it is not necessary to keep using range(len(x)) when you can just iterate over the list itself.

Hopefully you find the following ideas helpful:

for route in p_routes: length = len(route) if length > 2 : if route[2] == 'No Backup': K += 1 for z in range(length): nbup.write(K + 1, z, p_routes[i][z]) elif route[0][0] == 'E': L += 1 for z in range(length): ex.write(L, z, (p_routes[i][z])) elif route[0][0] == 'G': M += 1 for z in range(length): gh.write(M ,z, p_routes[i][z]) elif length == 2: print "It is equal to 2" else: print "It must be less than 2" 

Note, if x > 2 followed by an else, the else would imply the value is <= 2.

It is also possible to add one to a variable as follows: L += 1, this is shorthand for L = L + 1.

1 Comment

Thanks a lot for your valuable suggestion.. Yes the syntax error was coming from the non closing bracket indeed. I also figured out just after posting but was too bz doing the rest of the code completion. Now its working fine. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.