Is there a limit to the number of elif statements allowed?

Question

In Python, is there a limit to the number of elif statements that are allowed?

Answer

No, there is no strict limit to how many elif statements you can include after an if statement. You can add as many as you need into the program, not taking into account memory or other possible limitations like hardware.

The only strict limit is that there can only be one if and one else per if/elif/else statement block, but there is no limit on the number of elif.

16 Likes

https://bugs.python.org/issue16527

3 Likes

Why can’t I use an else statement? It causes an error. However, when I used the elif, it worked.

1 Like

Please post a link to the exercise where this problem is occurring. We can only speculate until we see your complete code submission, so please also post that in your reply.

2 Likes

Hello Sir,
I am having a doubt regarding the elif statement. Can you kindly bother to look at it and help me please!

def greater(x):
    if x > 5:
      return "the number is greater than 5"
    elif x > 7 :
      return "the number is greater than 7"
    elif x > 9 :
      return "the number is greater than 9"


greater(11)

when I am running this code, its not working and I am not able to recognize the mistake.

2 Likes

Remember that if-elif statements are evaluated in order and only if the preceding statement’s boolean condition was False. This means the if statement is evaluated first, then elif x > 7 if x > 5 == False, then elif x > 9 if x > 7 == False.

Let’s take your function call of greater(11). The if statement is evaluated first. Since 11 > 5 == True, the code inside the if block is executed. Now, the if-elif block is exited, meaning that both elif statements are not even evaluated. What changes could you make to return the desired output? There are several ways to accomplish this.

5 Likes

Thank you so much for the answer. Now I understood how it happens .
Also Can I just take the biggest number above so as to get the desired output for example in place of “5” maybe write “9” or is there some other way to solve this?

1 Like

The order matters, so generally speaking if greater is the criterion, then start with the largest, and work your way down.

6 Likes

There are many ways to solve this. I encourage you to play around with the code and try it out for yourself! Yes, putting if x > 9, then elif x > 7, then elif x > 5 would work since you are working from the largest number to the smallest number.

2 Likes

Oh ohkay , thank you so much.

2 Likes

Oh yes i did exactly that and it worked fine… Now I know what to do… Thank you so much…

3 Likes

grade = 86

if grade >= 90 :
  print("A")
  elif grade >= 80 :
      print("B")
    elif grade >= 70 :
        print('c')
      elif grade >= 60 :
          print("D")
        else :
            print('F')

Why is this giving me Error SyntaxError: invalid syntax

Should there be no spaces before elif and else? I have given it to beautify the code. Is it not acceptable here or any IDE where we write the python code.

I am new to Programming :slight_smile: Thank you!!!

Excessive and unmatched indentation. An elif is not nested in the if, it is parallel to it…

if ___:
    # code
elif ___:
    # code

Aside, the whitespace before the colon is valid, but nonstandard. Suggest don’t use whitespace in that position unless your teams style guide allows it. Better to form the habit of not using the above style.

grade = 86
if grade >= 90:
    print ("A")

Note how above both lines are along the same margin. Indentation has a specific impact on how the code is parsed and interpreted. It will take a little while to get used to it, but it is well worth the effort to concentrate on the correct use of indents.

1 Like

in case someone need more explanations for this question, i wish the following will help to gain more understanding of elif statements :
There is nothing syntactically wrong with the greater() function definition or when calling greater(11).

However, there is a logical error in the elif statements that would cause the function to not work as expected.

Specifically:

def greater(x):
    if x > 5:
        return "the number is greater than 5"
        
    elif x > 7: 
        return "the number is greater than 7"

    elif x > 9:
        return "the number is greater than 9"

The problem here is that if x is already greater than 5, it will execute the first return statement and never reach the subsequent elif statements.

For example:

  • greater(11) checks if 11 > 5. This is true, so it executes the return “the number is greater than 5”
  • It immediately returns from the function after the first true condition.
  • So it never even checks the other elif statements

To fix this:

def greater(x):
    if x > 9:
        return "the number is greater than 9"

    elif x > 7:  
        return "the number is greater than 7"

    elif x > 5:
        return "the number is greater than 5"

By arranging the conditions from highest to lowest, it ensures that the most appropriate message is returned.

1 Like