Reputation: 287
I am completely new to programming and have decided to begin my journey by learning Python. Along the way I have been having some issues with knowing how code is behaving with respect to indentation. I will give an example:
Problem:
Given an array of integers, return the number of 9's in the array.
My first attempt did this:
def array_count9(nums):
list_of_9 = []
for num in nums:
if num == 9:
list_of_9.append(1)
return sum(list_of_9)
This was wrong, and I discovered later (after trial and error) that the reason was because I hadn't properly indented my last line of code.
The correct answer should:
def array_count9(nums):
list_of_9 = []
for num in nums:
if num == 9:
list_of_9.append(1)
return sum(list_of_9)
This is a problem I have run into repeatedly, and I think it's because I don't really understand how Python deals with indentation.
I understand now that the correct answer is correct because with my first attempt, I am telling Python to return the sum of the objects inside the list immediately after testing whether or not the first integer is 9. In the correct example, Python will run through all the numbers, and only when it is done, go back out to the previous indentation and return the list.
I don't know, I guess I am wondering if someone can provide a "walk-through" of what is happening in each line of good or direct me to a resource that discusses this further. I have looked for such a resource online but have been unable to find one.
Any help would be appreciated!
Upvotes: 0
Views: 126
Reputation: 131
In many languages, indentation is used to make the code look neat and easy to read. However, this is not the case with Python. In Python, indentation is required for indicating what block of code a statement belongs to. For instance, let's look at the code below:
for i in range(0,5):
print(i)
print("All done!")
The statement print(i)
is indented under the for-loop; this means that the statement is under the for-loop. Therefore, the statement will be executed 5 times. Since the statement print("All done!")
is not indented, it is not under the for-loop; therefore, it will only be executed once. The output for the above code is
0
1
2
3
4
All done!
Now, let's modify the code a little bit.
for i in range(0,5):
print(i)
print("All done!")
Now, since both the print statements are indented under the for-loop, they will be executed 5 times. Therefore, the output now will be this:
0
All done!
1
All done!
2
All done!
3
All done!
4
All done!
Now, let's look at the initial version of your code.
def array_count9(nums):
list_of_9 = []
for num in nums:
if num == 9:
list_of_9.append(1)
return sum(list_of_9)
You have the return statement under the for-loop, due to the indentation. This means for every iteration of the for-loop, the return statement will be called. However, since return statements end the function, the for-loop stops at its first iteration. You want to return the sum of the list_of_9 array after iterating through nums. Therefore, the return statement will be after the for-loop, thereby not being indented under the for-loop. That is why the following corrected version of yours works:
def array_count9(nums):
list_of_9 = []
for num in nums:
if num == 9:
list_of_9.append(1)
return sum(list_of_9)
Upvotes: 1