FAQ: Control Flow - Else If Statements

That gives an initial value which if unchanged by the final return statement will be the default return value.

The problem with the above code is the value never changes since the grades are only printed, not returned. Change all those print statements to,

grade = "A"
...
grade = "D"

Now the values will be changed (or not) before the return statement.

def grade_converter(gpa):
grade = “F”

if gpa >= 4.0:
grade = “A”
elif gpa >= 3.0:
grade = “B”
elif gpa >= 2.0:
grade = “C”
elif gpa >= 1.0:
grade = “D”
elif gpa >= 0.0:
return ‘F’

print(grade_converter(4.8))

in the example question with donations that goes along with the elif exercise it states that if the elif statements were just if, then it would print all the possible return statements, which it does

But why when I change all of the elif statements in the code above, it still returns just one answer?

Thanks

there is a difference between print and assigning to a variable. In the latter case, the variable just gets overwritten every time, still resulting in a single value. print will print each time

the ultimate test would be to combine the exercise with the example question (have assignment and print at each if clause.

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")

May I know why I still get error even I have followed the instruction & example?

This is the error:

  File "script.py", line 7
    elif grade >= 70:
       ^
SyntaxError: invalid syntax

elif has to be after an if or another elif, this elif is nested:

  elif grade >= 70:

as are all elifs and else after it. That doesn’t work.

1 Like

8 posts were split to a new topic: Control flow - Else if statementss

Why is this considered incorrect?

grade = 86 if grade >= 90: print("A") elif grade >= 80 and grade < 90: print("B") elif grade >= 70 and grade < 80: print("C") elif grade >= 60 and grade < 70: print("D") else: print("F")

If you do not include the “and” statement and the order of checks is different from “top to bottom”, would it not result in printing multiple responses? You need the “and” statements to include an upper bound on the grade classification or else a grade of 86 would meet multiple criteria (i.e. 86 is greater than 80 as well as 70, 60, etc), right?

Edit: I just checked it by changing the order of “if” checks without the “and” statement, and the “correct” code will produce an incorrect answer.

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

Here, the B and C check are reverse in order and since 86 is greater than 70, it classifies an 86 as a C, which is incorrect.

The problem with this exercise is that their solution depends upon the user listing their “if” statements from A down to F.

1 Like

Top down or bottom up won’t matter so long as the sequence is maintained. We cannot get to the fourth floor without first going to the lower three floors, in order. Nor can we get to the first floor without going through the floors above it.

There is no required extra logic so long as the sequence order is ascending or descending.

if x < 60: # F
if x < 70: # D
if x < 80: # C
if x < 90: # B
# A

The cascade gives us a process of elimination.

Is it a good idea though to assume the user writes the sequence in ascending or descending order though?

The user isn’t writing the code, we are. Code is written using the best reasoning of the programmer. So long as the relational operators are correctly chosen, and there are no assumptions, the language promises to interpret our code the way we write it.

When do we use the nested if statements and when do we use if, elif, else? what is the difference?


Hello can you please explain why do i get error?

1 Like

Inconsistent indentation. All the elifs and the else must line up with the if.

2 Likes

I was siightly baffled by this also: I don’t recall any discussion in the lessons so far about the importance of whitespace, and how Python deals with it (but maybe I missed it).

Doesn’t it seem weird that the editor automatically inserts these indentations? If whitespace is so important in Python (which it appears to be), then why would the editor be adding it at returns?

1 Like

Wish I had a good answer to that question, but I don’t. Early on I quit relying on editors and just took personal responsibility for indentation consistency, even to the point of manually redoing all of them with four spaces per nested level.

One suspects that a decent editor will give us the option of what to use for indentation (space or tab) and the default size of a single indent. This might enable the tab key to insert spaces, not a tab character (ASCII 12). Without that special configuration we don’t know what to trust, so trust yourself.

Hi. I just want to say that the English instructions for the ‘Else If Statements’ section are badly written and difficult to follow. I suggest someone at Code Academy proofreads them, breaks them down, and improves them. It’s disconcerting for learners given that learning Python 3 is hard enough, without seeing instructions that are convoluted and difficult to make out and to follow. Thanks.

You could post this under the Feedback & Requests category. (With a link or screenshot to support your issue). It might get more traction there.

if, elif, else:
This construct is particularly useful when you have multiple conditions that are mutually exclusive, meaning only one of them should be executed. The elif (short for “else if”) allows you to check multiple conditions in a structured way. Here’s an example:

x = 10

if x > 10:
    print("x is greater than 10")
elif x < 10:
    print("x is less than 10")
else:
    print("x is equal to 10")

In this example, only one of the blocks inside the if , elif , else construct will be executed, depending on the value of x .

Nested if statements:

Nested if statements are used when you need to check additional conditions within one of the blocks of an if statement. Here’s an example:

x = 10

if x == 10:
    print("x is equal to 10")
    
    if x % 2 == 0:
        print("x is even")
    else:
        print("x is odd")
else:
    print("x is not equal to 10")

In this example, if x is equal to 10, it goes into the first block and then checks whether x is even or odd with a nested if statement.

Conclusion

  • Use if, elif, else when you have mutually exclusive conditions, and you want to execute only one block of code based on the first true condition encountered.
  • Use nested if statements when you need to check additional conditions within a specific branch of your decision-making process.
1 Like

Why does my code throws an indentation error, in the sense that why does all the conditional statements need to be flushed?

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”)

I input them with different indentations, but it is displayed here as flushed.

The answer to your question is likely to be found in the new user category, something to the effect of, how to post code. Do take a look, if you will.

1 Like