Difference

What will be the difference between these two codes

def ... :
 if ...:
  return ....

 if... :
  return...

 if ...:
  return ....

 if... :
  return...

else:
 return


and

def ... :
 elif ...:
  return ....

 elif... :
  return...

 elif ...:
  return ....

 elif... :
  return...

else:
 return

I hope I have presented my problem clearly enough :slight_smile:

Both are invalid code. Your “else” clause isn’t attached to an “if” or “elif” because of incorrect indentation.

However, if you had indented them correctly…

The multiple ifs will cause the program to check each and every if. If the condition is true, it will execute the code, otherwise, it will move on.

The multiple elifs is still invalid because there’s no if! If you had added an if statement, then it would check the if statement first. If the condition was true, the block inside would execute, and then it would ignore all of the elifs and the final else in the “chain”. If it was false, then it would move on to the first elif. The same cycle. If the elif’s condition is true, then its block will execute and the rest of the elifs and else will be disregarded.

Hope this helps. I also recommend you to review logic and if statements in Python.

1 Like