Nested If statement returning other blocks?

The letter grade B is selected for output but I am not sure why the indented block for grade A is printed as well? Can you explain how to fix this? Thanks. Anything other than the value grade of A plugged into the grade variable will print the other statements.

link to the workspace: https://www.codecademy.com/workspaces/61059897ffe4a050171cd3f3

test_value = 80

if test_value >= 90:

print(“I got an A grade”)

else:

if test_value >= 80:

print("I got a B grade")

else:

if test_value >= 70:

  print("I got a C")

else:

  print("I got an F")



print("I got a grade of C, therefore I did not an A, B or F")    

print(“I got a grade of B, therefore I did not get an A, C or F”)

print(“I got an A, therefore I did not get a grade of B, C or F”)

Hi :slight_smile:,

I’m going to re-post here with a bit wider indentation (you can do 4-spaces in python as long as it’s consistent)

test_value = 80

if test_value >= 90:
    print("I got an A grade")
else:
    if test_value >= 80:
        print("I got a B grade")
    else:
        if test_value >= 70:
            print("I got a C")
        else:
            print("I got an F")
    
        print("I got a grade of C, therefore I did not an A, B or F")    
  
    print("I got a grade of B, therefore I did not get an A, C or F")

print("I got an A, therefore I did not get a grade of B, C or F")

Notice how the last line print("I got an A, therefore I did not get a grade of B, C or F") aligns with the original if-block, meaning: it will run no matter what number you put (even if you put a non-number!).

Please see this link for info abut formatting posts: [How to] Format code in posts

2 Likes