I am a total beginner and I thought I understood indentation.
I do not understand as to why the 1st block gets indentation but the 2nd does not?
x = “uh huh”
def myfunc():
x = “sure”
print("life is " +x)
myfunc()
print("Life is " +x) # I am looking at this print function and myfunc() directly above it. Why no indentation?
When I ‘run’ print with an indent I get an indent error…
please use the actual code formatting, it is very easy to do. Three backticks around the block (the key with the ~ on it, on a US keyboard). You can check the preview of your post to make sure it works.
```print("life is " +x)
myfunc()
print("Life is " +x) (THIS BLOCK DOESN'T GET AN INDENT?)
The 1st block is indented:
x = “uh huh”
def myfunc():
```x = “sure”
```print("life is " +x)
Indentation is used to denote a block of code, as in the code following the function definition def myfunc(): (note the colon at the end of the line, telling us that what follows will be a new block of code)
There’s no indentation after the call to myfunc because calling a function doesn’t denote the beginning of a new block of code.
Other situations where you would indent a block are conditional statements:
if something_is_true:
do_this_when_true()
else:
do_something_else()
Or in for loops (for i in range(10): ...) or many other cases. In general, when you see a : at the end of a line, that’s a sign that a new block is starting.
I should just add that the colon character is used in other places too (like dictionaries). That explanation is a useful rule of thumb for getting started, but as you get more familiar with Python I wouldn’t want you think I lied to you.