I want to write progarm, The function accepts two integers n, m as arguments Find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with the sum of numbers divisible by n. In my case, if is not working.
n = int(input("num "))
m = int(input("limit "))
for i in range(1, m 1):
sum1 = 0
sum2 = 0
if i % n == 0:
sum1 = i
else:
sum2 = i
print(f"{sum1} and {sum2}")
print(abs(sum2-sum1))
CodePudding user response:
Take sum1 = 0
and sum2 = 0
outside the for loop; currently you are resetting those values at each iteration, so that it does not keep the sum.
n, m = 2, 10
sum1 = 0
sum2 = 0
for i in range(1, m 1):
if i % n == 0:
sum1 = i
else:
sum2 = i
print(f"{sum1} and {sum2}")
print(abs(sum2-sum1))
Output:
30 and 25
5
CodePudding user response:
Bring your sum1 and sum2 outside of the loop, you're resetting them every time