I can't figure out why this is wrong (Control Flow Advanced Challenges)

I keep getting syntax error for every challenge I do, even though the code is correct (I checked the solutions). I’ve tried changing the indentation but the codecademy interface makes this so difficult. I press return after each if and else statement and it does some sort of auto-indent. This is how my code looks - i’m hitting the return key after each line of code:

# Write your in_range function here: def in_range(num, lower, upper): if(num >= lower and num <= upper): return True else: return False # Uncomment these function calls to test your in_range function: print(in_range(10, 10, 10)) # should print True print(in_range(5, 10, 20)) # should print False

For the second challnege:

# Write your same_name function here: def same_name(your_name, my_name): if your_name == my_name: return True else: return False # Uncomment these function calls to test your same_name function: print(same_name("Colby", "Colby")) # should print True print(same_name("Tina", "Amber")) # should print False

Third challenge:

# Write your always_false function here: def always_false(num): if num > num and num < num return True else: return False # Uncomment these function calls to test your always_false function: print(always_false(0)) # should print False print(always_false(-1)) # should print False print(always_false(1)) # should print False

Welcome to the forums.
Debugging can be tricky sometimes b/c we look at the same code over and over and over. Maybe try reading the code from the bottom up to give you a different perspective?

For the first & second ones, you have an indentation issue, as the error points out at the else: part of the logic.
For the third one, the issue is at the end of your if, as the SyntaxError points out.


Shouldn’t the code editor automatically indent correctly after I press the return key? How am I supposed to know the correct level of indentation if doing manually.

I don’t know how the code editor is set up (in the back-end) in the module challenges. Do you have a link to this? (is it in the Python course or Data Science?)

Indentation matters in Python (unlike other programming languages) b/c it tells the compiler what blocks of code to execute and what statements belong to which blocks of code. if and else should be 4 spaces.
Sometimes error messages are difficult to understand, but, with the first one, you can see that the error lies with the else: statement. else needs to be at the same level of indentation as if. I also wouldn’t rely on the code editor for indentation (see below) even though some IDEs do automatically indent for you, you should still understand how their code is structured.

Here’s a brief explanation of it: https://www.geeksforgeeks.org/indentation-in-python/

and here too: https://www.askpython.com/python/python-indentation

Thanks for the reply. I understand indendation is important and necessary for the compiler to recognise specific code blocks for proper execution, but my issue is I don’t understand why when I write my code, it’s not indenting properly.

I am doing the data science python code challenges: https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-control-flow/articles/advanced-python-code-challenges-control-flow

Previously it would indent correctly in other lessons, but it’s not now, observe:

I have no idea why the functionality is different between the challenge lessons. Maybe they changed something in the back end of the code…and they want people to manually indent rather than rely on an IDE that automatically does it(?)

Hey @robinjb_n7

I’ve tried this in the challenges you linked to, as well as a couple of lessons in the Python 3 track and in Visual Studio Code on my own machines.

In each case, the editor correctly increases the indent when we enter a new block - so writing if {condition}: results in an increase in the indentation when we move to the next line.

What doesn’t happen in any of the cases is the editor automatically decreasing the indent, meaning that when we’re finished with our if we need to manually decrease the indent by hitting backspace to get out of that block.

FWIW, I don’t recall the CC editor or VSC ever doing the un-indent for me automatically… but since Python relies solely on indentation to dictate code-blocks, it may be harder to programmatically work out when the block ought to end. In a language which demarcates it’s blocks with curly braces, such as JS, the editor has additional info to know when to decrease an indentation for readability (because you’ve typed }).

1 Like

Ok so it seems I have to manually decrease the indent every time I want to exit a code block? For some reason I’m sure the editor did it for me in the past, that’s why I’m so frustrated, but I could be mistaken. I guess it’s designed this way on purpose. Thanks for checking this out.

Hm. I don’t ever recall the CC code editor automatically decreasing indentations. I could be wrong…but it’s not HAL. It can’t read my mind when I’m writing code. (If it can, then we’re in trouble)…

I use Colab and I’ve always had to manually decrease there. Same with Jupyter Notebook.
I don’t know of any IDE where one wouldn’t have to manually decrease.

This is my first time using the CC editor so it’s probably me that’s wrong. I’ve used VS and simple editors like notepad++ and brackets with other languages and I was able to configure the indentation.
I’m going to be exploring Jupyter soon so hopefully this will give me some more insight and familiarity working with python!

On line 5, align your elif statement with your if statement. Do the same with the else portion on line 7 as well.