Python: Code Challenges Loops: 1. Divisible By Ten - Error?

https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-loops

Hi,

I have just completed a topic on Functions in Python 3 course. Up until then, I found Codecademy very beneficial and encouraging - that was until I started to complete the Challenges where I realised my knowledge was nowhere near adequate to complete some of them (Lists & Loops namely). I think there is a huge gap between the examples in the learning section and the exercises in the challenges and I wonder if there is a source that can better equip me to solve these challenges…

So, I’ve been mostly copy-pasting the solutions into the terminal and hoping to understand the concept by staring at the code for ages. Then I reached the exercise titled, and after running the solution code provided, it kept returning a different value from the expected one. When I clicked “Check answer”, it confirmed a different value was expected. Does anyone have an idea where the problem is? Thanks a lot for your help!

def divisible_by_ten(nums):
  count = 0
  for number in nums:
    if (number % 10 == 0):
      count += 1
    return count

print(divisible_by_ten([20, 25, 30, 35, 40]))

This code prints 1, but the expected value is 3.

Many thanks again!

It happens.
When you were trying to debug your code, what’s your process? Did you tweak the indentation at all? What happened when you moved pieces of the code around?

Can you explain what the function is supposed to do? If you’re presented with a question like this and you don’t quite understand (which it totally normal!), break it out into pieces and in words that make sense to you. Draw it out if it helps too.

That said, are you able to explain why the count returned is 1? Remember, for loops iterate, if statements are conditionals and do not iterate through a list.
Check the indentation on your return value in your for loop. What happens if you move it (outdent it) and, more importantly, why?

1 Like

Hey Lisa,

Thanks a lot for your help!

The truth is I didn’t try to debug the code, because it was the code coppied from the solution provided and I wouldn’t be able to put it together myself, not to mention debug it :smiley:

But after reading your response I see how examining the indentation would be the low hanging “debugging fruit” - and indeed, the formatting didn’t copy correctly - I was able to get the correct value by unindenting return as you suggested - again, thanks a lot for that - a lesson to keep :slight_smile:

My guess is the reason why I was getting 1 with the former indentation was because the loop stopped at the first index…? Whereas correcting the indentation let the loop go through the whole list…?

I do try to break the assignments down to the dot before (and during) any coding attempts. I hope I will get better as I put more miles in.

This really helped, so thanks again!

1 Like

Yep. It stopped after the first “number” in “nums”. When that if condition is met (“is the number in the list divisible by 10? if so, add it to the counter”, the return is executed and then function terminates b/c of where that return statement is indented. SO, the return needs to be at the same level as the for loop, so the logic loops through the entire list provided/passed through the function.

Debugging is a skill that you will keep improving upon. It takes time! :slight_smile: Write the code, break it, figure out what works (and why), and what doesn’t work (and why). If you can explain your code to a duck, then it’s all good. :duck:

Awesome, thank you for the breakdown! It does make a lot more sense now :slight_smile:

1 Like