Hey Tech Rocketeers!
We have been noticing a number of posts that have sprung up from issues with indenting in your code challenges. I’m here to help with a simple explanation that will hopefully help you out!
Indenting is needed when a line (or lines) of code are a part of another piece of code, like an if statement, loop, or function. It tells the compiler that everything indented below a line of code is a piece of that statement, loop, or function.
This could be the code of a challenge you encounter:
The Main function runs your program. Notice that the Armory Variable and If / Elif / Else Statements are indented from one tab from the Main function (Yellow Line). This is because each of these items are individual parts of code that make up the Main Function. Though they have code that makes up their own structure, they are each a part of the Main function.
So now we'll look at the second set of code, the variable, if, elif, and else statements.
- The variable has no code that makes it up, so there is no next level to indent to.
- The Elif statement prints "Armory has something!" when the value of armory is greater than zero. Therefore, that code is indented one tab from the elif statement. The same goes for the other print statement in the else statement. They each make up the statement that they are a part of.
Indenting is not only a way of organizing your code so it is cleaner and keeps you from getting lost, but also define how code is run. This means that proper indenting is EXTREMELY IMPORTANT!!!
The example above is Python, but it is also the same across all programming languages :
Also, if you are having trouble getting the indents right in code challenges, it is best to click your cursor next to the beginning of your line of code, hit backspace until your code goes to the previous line, and then hit enter. The code challenge will typically auto-indent to the next line.
Good luck out there!