-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?

Brandon Ginley
  • 249
  • 3
  • 12
  • Indentation is important in Python, unlike most other languages. The if and else need to be indented at the same level to be valid. – Stewart Jun 03 '15 at 17:35
  • 1
    Have you tried the [tutorial](https://docs.python.org/3.4/tutorial/controlflow.html)? Your indexing also looks funky. `if..else` troubles aside, does that part work? – TigerhawkT3 Jun 03 '15 at 17:38

4 Answers4

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.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • You could add to your answer the difference of using `elif` and `else: if`. – heltonbiker Jun 03 '15 at 17:36
  • @heltonbiker There is a good writeup of that [here](https://stackoverflow.com/questions/3742580/python-why-elif-keyword) so I will defer to that – Cory Kramer Jun 03 '15 at 17:38
  • 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. – heltonbiker Jun 03 '15 at 17:39
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
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • Also, since both tests are mutually exclusive, the OP could do straigth with an `else`, or even with no additional test at all. – heltonbiker Jun 03 '15 at 17:37
  • @heltonbiker And if there are more clauses? What if there's an extra condition. Then unwanted execution will take place. – Zizouz212 Jun 03 '15 at 17:38
  • 1
    @heltonbiker In most cases they *should* be mutually exclusive, but they might not be. [See this](http://stackoverflow.com/questions/9452536/why-does-python-have-an-ne-operator-method-instead-of-just-eq), for example. – Stefan Pochmann Jun 03 '15 at 17:48
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.

Pralhad Narsinh Sonar
  • 1,406
  • 1
  • 14
  • 23
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