Indenting a Print Statement

Working on Introduction to Functions in Python 3 in Codecademy Pro. Not sure why the 2nd print statement is running. If it’s indented the code shouldn’t run is my understanding. The code is formatted.

def about_this_computer():
	           print("This computer is running on Whackintosh   version Everest Puma")
print("This is your desktop")

For a section of code to be considered part of a function every line has to be indented.

Your second print statement is not indented, as such the computer will run as part of the
program.

I hope this helps :slight_smile:

2 Likes

To expand upon the correct answer given, Python uses indentation rules to help determine how a group of code relates to the rest of the program.

The second print statement matches the indentation level of the function definition (where you used the def statement). As such, the second print statement isn’t part of the body of the function.

Some follow-up remarks: PEP 8 recommends you use four spaces to represent an indentation level. The indentation level you’ve used (the equivalent of 15 spaces) can make simple edits more difficult. Most text editors can convert tabs for you to spaces (tabs are equivalent to 4 spaces). Also, the Python interpreter can get confused when mixing tabs and spaces in defining indentation, like you’ve done in your function. Mixing indentation methods can be a very difficult problem to diagnose should errors arise from it.

1 Like