Given two integers a and b, which can be positive or negative, find the sum of all the numbers between including them too and return it. If the two numbers are equal return a or b.
Note: a and b are not ordered!
def get_sum(a,b):
#good luck!
if a == b:
return a
else:
if a > b:
a, b = b, a
result = 0
for num in range(a,b+1):
result += num
return result
First judge whether the two numbers are equal, the two numbers are equal, return a
is not equal, return the accumulation result
Because range() is closed before and open, the accumulation needs to include b, and the boundary is b+1
Note: a and b are not ordered!
a, b are unordered, if you do not judge the size and use it directly, it will lead to the possibility of result=0 to
judge the size
if a > b:
a, b = b, a
Python automatically packs and unpacks