-6

In my python code, I am attempting to implement an if..else statement. See below:

 if index[y] == index[x]:
 continue
 else index[y] != index[x]
 indexdn = indexd
 indadd= index[y]-index[x]
 indexdn[:,y]=indexdn[:,y]+ indadd
 index[y,:]=-indexdn[:,y]
 indexdn[y,y]=0

However, no matter how many different ways I attempt to write else index[x] is not equal to index[y] I get a syntax error on return of this line. I have tried using else, elif, and for the not operand, != and 'is not'. What is the proper way to write a Python statement using a "not equal" operand so that I do not receive a syntax error?

asked Jun 3, 2015 at 17:33
2
  • Indentation is important in Python, unlike most other languages. The if and else need to be indented at the same level to be valid. Commented Jun 3, 2015 at 17:35
  • 1
    Have you tried the tutorial? Your indexing also looks funky. if..else troubles aside, does that part work? Commented Jun 3, 2015 at 17:38

4 Answers 4

3

The correct syntax is

if condition:
 # stuff
elif other:
 # stuff
elif some_other:
 # stuff
else:
 # stuff

Note that else does not get any explicit condition, it is the catch-all if none of the above conditions were True.

answered Jun 3, 2015 at 17:34
Sign up to request clarification or add additional context in comments.

3 Comments

You could add to your answer the difference of using elif and else: if.
@heltonbiker There is a good writeup of that here so I will defer to that
Yeah, my comment was just to remind the few cases where you actually want nested ifs instead of a single, switch/case-like set of conditions, since they are conceptually different.
2

Just fix your indentation, change else to elif and add the missing colon:

if index[y] == index[x]:
 continue
elif index[y] != index[x]: # indentation and colon
 indexdn = indexd
 indadd = index[y] - index[x]
 indexdn[:,y] = indexdn[:,y]+ indadd
 index[y,:] = -indexdn[:,y]
 indexdn[y,y] = 0
answered Jun 3, 2015 at 17:36

3 Comments

Also, since both tests are mutually exclusive, the OP could do straigth with an else, or even with no additional test at all.
@heltonbiker And if there are more clauses? What if there's an extra condition. Then unwanted execution will take place.
@heltonbiker In most cases they should be mutually exclusive, but they might not be. See this, for example.
2

I think you are missing upon 2 important things 1. Intendation and 2. Colon after else

if index[y] == index[x]:
 continue
else:
 indexdn = indexd
 indadd= index[y]-index[x]
 indexdn[:,y]=indexdn[:,y]+ indadd
 index[y,:]=-indexdn[:,y]
 indexdn[y,y]=0

By going by the example given by you - no comparison is required in the else.

answered Jun 3, 2015 at 17:43

1 Comment

@Brandon Ginley - please let me know if there is any further scope that you are looking after to implement if...else in python.
0

I guess you use this in a loop, but if you dont you should use the keyword pass instead continue.

if index[y] == index[x]:
 pass
elif: # indentation and colon
 indexdn = indexd
 indadd = index[y] - index[x]
 indexdn[:, y] = indexdn[:, y] + indadd
 index[y, :] = -indexdn[:, y]
 indexdn[y, y] = 0

However, if it isn't in loop, you need only:

if index[y] != index[x]:
 indexdn = indexd
 indadd = index[y] - index[x]
 indexdn[:, y] = indexdn[:, y] + indadd
 index[y, :] = -indexdn[:, y]
 indexdn[y, y] = 0
answered Jun 4, 2015 at 2:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.