FAQ: Code Challenge: Loops - Divisible by Ten

well, if we call the function multiple times:

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

we can indeed see that for the second function call, we get 6. The counting just keeps going instead of resetting.

Hi !
I hope I could find some explanation here because I’m stuck.

I don’t understand why this message appears when I run my code

" divisible_by_ten([]) should have returned 0 , and it returned None "

while what is returned is actually “3”.

My code is not the best one but it works (I also put !=0 to see how many not divisible numbers were found with solution and the result is also correct (2)

def divisible_by_ten(nums):
div_by_ten = 0
for r in range(len(nums)):
if r <= len(nums) - 1 :
for i in nums:
if i % 10 == 0:
div_by_ten += 1
else:
continue
return div_by_ten

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

if we add the function call which gives an error to your program:

def divisible_by_ten(nums):
div_by_ten = 0
for r in range(len(nums)):
if r <= len(nums) - 1 :
for i in nums:
if i % 10 == 0:
div_by_ten += 1
else:
continue
return div_by_ten

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

i do see None, the absence of a return value.

Hi thank you for your replies ! I have understood my mistakes thanks to you and other people !
And also that I make things too complicated and why I do so
I’ll try to change that !

I have the same problem and I still don’t understand it. The output is 3 so why does it say it’s wrong? How to fix it?

without any context (code, errors) its really difficult to answer the question. Your problem might be different.

I wrote the same code as css1270427778, I have the same problem as them, I don’t understand your explanation and I don’t know how to fix it.

can i see your code? This way, I can verify you have the same problem.

What do you not understand about my explanation? Did you add the second function call like I showed to css127? What results does this yield? And what does this tell you about your code?

Hi all!
Is there more elegant way in solving this task, than just creating the variable before cycle?
Like it was written higher in many tries.
Thanks in advance for any help!

The examples posted above have one thing in common… They return a count. We can derive that count in a number ways, including those above. We do need to iterate the list and evaluate each element. The most common approach is to use for and accumulate a count of the number of times the condition is met. Something so simple is hard to make more elegant.

return sum([1 for x in nums if x % 10 == 0])

Mind, if the built-in sum() function is unknown or list comprehensions have not been covered yet, this is not a valid solution to the exercise. We should use what we know at each stage and leave improvement for the review process once completing the track. Let the basics sink in and gain proficiency before tossing them aside in favor of more elegant solutions. There will be lots of time for that in the future.

Blimey, aside from the variable names, that’s exactly the same code I wrote!

Then we have this approach,

>>> def ten_goes_into(nums):
return sum([not x % 10 for x in nums])

>>> ten_goes_into([12, 20, 25, 30, 50, 70, 85])
4
>>>

All that remains now is making sure our program is not being fed garbage. This is the process of argument validation which becomes our highest priority when dealing with uncontrolled data such as user inputs.

>>> def is_number(x):
try:
y = x / 0
except ZeroDivisionError:
return True
except:
return False

>>> is_number(42)
True
>>> is_number('42')
False
>>> def ten_goes_into(nums):
return sum([not x % 10 for x in filter(lambda n: is_number(n), nums)])

>>> ten_goes_into([12, 20, 25, 30, 50, '70', 85])
3
>>>

number % 0
and not
nums % 0
thx

def divisible_by_ten(nums):

count = 0

for number in nums:

if (number % 10 == 0):

count += 1

return count

number is a value in the nums list. We cannot divide into a list.

number / 10

versus,

nums / 10   # will raise an exception

oh, i see, thank you

1 Like

Why exactly does the modulus operator need to be in parentheses here? This was the code I wrote. It’s identical to the solution code with the one lone difference being that the i % 10 == 0 statement isn’t in parentheses.

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

Can someone please explain the difference between

if i % 10 == 0:

and

if (i % 10 == 0):

The example text in the opening to this exercise doesn’t use parentheses.

No difference. The parentheses aren’t needed, so then preferable omit them.

My (inelegant and inefficient) solution:

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

Why is it inelegant or inefficient? The else is redundant, but for the rest is it readable clean code

Well tbh this is my first time posting my code to the forum and I figured I should be as humble (and/or self-deprecating) as possible.