 # Divisible by 10 using while loop

in the divisible by 10 problem in loops , how to use the while loop to get the output.
can some one help in debugging this code.
def divisible_by_ten(nums):
n=0
count=0
#while n<len(divisible_by_ten):
#or
#while n in range(len(divisible_by_ten)):
if divisible_by_ten[n] %10==0:
count+=1
return count

#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))

the error i am getting is ““TypeError: object of type ‘function’ has no len()””
which is happening in while loop declaration

I remember using code like below once but it too seems to give invalid syntax
count=0

def divisible_by_ten(nums):

for n in nums if n%10==0:

``````count+=1
``````

return count

In the for, you use `nums`, the parameter of the function.

In the while loop, you use `divisible_by_ten`, the function itself.

1 Like

Hello @text7397673145. You can’t use the length of a function, but you can use the length of the `return` value fo a function. I can’t see much more without seeing the indenting of your code. To format your code correctly, press this icon

.
Also, with this part

you can just write

``````while n < len("""item""") or n in range(len("""item""")):
#code
``````

This can work, but there is a problem with the scope of the variable `count`. Most likely, the error thrown by this code would be `undefined: local variable 'count' is undefined, line x`, or something along those lines. To fix this, you can have `count` inside your function:

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

I hope this helps!

2 Likes

thanks. i was using the function name , not the list name.

1 Like
``````def divisible_by_ten(nums):
n=0
count=0
while n<len(nums):
if nums[n] %10==0:
n+=1
count+=1
return count
print(divisible_by_ten([20, 25, 30, 35, 40]))
``````

still its not printing , no errors nothings , i dont know maybe it became a infinite loop

Blockquote

You have an infinity loop, `n` (which keeps tracks of loop progress) is only updated when a number a number is divisible by 10, so what happens when a number is encountered that isn’t divisible by 10?

I would recommend using a for loop, the amount of iterations are known so the for loop is the better fit

3 Likes

If you’re really stuck and want to see how I attempted it, my solution is below.

Here is how I did it.
``````#Write your function here
def divisible_by_ten(nums):
list = []
for num in nums:
if num % 10 == 0 :
list.append(num)
return len(list)
#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))
``````
1 Like