- Find the sum of all multiples of n below m
- Keep in Mind n and m are natural numbers (positive integers) m is
excluded from the multiples - sumMul(2, 9) ==> 2 + 4 + 6 + 8 = 20
- sumMul(3, 13) ==> 3 + 6 + 9 + 12 = 30
- sumMul(4, -7) ==> "INVALID"
I did sum of list using range(n, m, n) using n as step.
I also tried modulus to avoid range 3 args error.
I can pass many tests but cannot pass all of them.
I have tried lots of logic but to no avail. What I am doing wrong?
CODEWARS: https://www.codewars.com/kata/57241e0f440cd279b5000829/train/python
MY CODE:
def sum_mul(n, m):
my_list = [number for number in range(n, m) if number % n == 0]
sum_list = sum(my_list)
if sum_list >= 1:
return sum_list
elif n == 0 and m == 0:
return 'INVALID'
elif n == m:
return n - m
elif n > m:
return 'INVALID'
You have one main problem, that is you should prevent the situation when
n==0and you divide it in your list comprehension. It will raisezero division error. so you should check before the validation that n is not equal to zero. Second thing is that you need to check whethern or mare negatives, as the exercise declared both n and m should be positives.