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

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

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

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! 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.

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

1 Like