Better not to use if-elif-else syntax?

Hi everyone! I’m relatively new to Python and don’t have a strong understanding of some best practices in syntax/formatting. Some of the early exercises in the Python track recommend using the if-elif-else syntax for conditional formatting, but the Pylint analyzer seems to not like that. I’ve seen other Community Solutions simply use “if” statements, leaving out elif and else altogether.

For example, from the Meltdown Mitigation exercise:

if (temperature * neutrons_produced_per_second) < ((threshold * 9.0) / 10.0):
    return 'LOW'
if (temperature * neutrons_produced_per_second) < ((threshold * 11.0) / 10.0):
    return 'NORMAL'
return 'DANGER'

Can anyone help explain the best way for me to go forward with these types of if statements? Thank you in advance for your help!

From a programming perspective, I am seeing ranges here, and so it suggests that perhaps a switch or case statement (whatever the language calls such a thing) is perhaps warranted here.

There may be other language specific solutions for patterns of this kind.

Remember that depending on the exercise, if you are in Learn or Practice side of the track, some exercises are specifically encouraging use of a specific concept, and that may not translate to an interpretation of “always use”.

I hope that helps you find what you are looking for.

1 Like

There is significant differences between when code has a return and when it does not. When a return is encountered, the function exits and nothing else runs. When there is a return inside and if, the if should not be followed by an else (or elif).

These are functionally identical:

if condition:
  return one
else:
  return two

if condition:
  return one
return two

A return should not be followed by an else (or elif).

When there is no return, none of this applies and else or elif makes sense.

Does that help?

1 Like

Oh okay, so basically after a return the program doesn’t even encounter the elif or else statements so they’re unnecessary. That makes sense and explains some of the Community Solutions I’ve been seeing. Thank you for your help!

1 Like

Exactly. Or, more generally, the program doesn’t encounter anything in a function after a return.

2 Likes

As a side note: Asking this question in an unsafe environment may cause a flame war, similar to “spaces or tabs for indentation”.

There is a school of thought that prefers complete if/else constructs to keep the structure of the selection intact: This is the code block for this condition and that is the code block for the rest of the cases. As mentioned, the else is “unnecessary” for the functionality. But leaving it out is also hiding possibly relevant information that has to be infered from the return interpretation.

I personally don’t favour that thinking, I also prefer the “no else” style.

2 Likes

The Python track (and Python in the industry in general) uses pylint or similar linters to check the code. By default most Python linters enforce no else after return and most people don’t turn that off.

Personal preferences aside, I think it’s safe to say the industry standard in Python is to not include the else.

1 Like