Code challenge loop : divisible by ten - output is 6 when it print 3


#1

hello there,

I was doing the “code challenge loop : divisible by ten”
https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/lessons/python-functions-loops-cc/exercises/divisible-by-ten

I know my method was a weird one (guess I was obsessed with list at this point), I changed it since, but I still don’t understand the error message in this one

MY CODE :

lst = []
def divisible_by_ten(nums):
for x in nums :
if x%10 == 0:
lst.append(x)
return len(lst)

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

the console print “3”

The error message say : “”divisible_by_ten([20, 25, 30, 35, 40]) should have returned 3 , and it returned 6""

any idea?


#2

see what happens when you add a second function call


#3

oh, indeed !

thanks !
would reset lst into the function fix the problem ?

def divisible_by_ten(nums):
lst = []
for x in nums :
if x%10 == 0:
lst.append(x)
return len(lst)


#4

that should work, then you have an empty list you are appending to the second (third, fourth and so forth) function call. No leftovers from earlier function calls.