看看人家写的代码
输入两个数A,B,如果相等则返回AorB,如果不相等则返回小的到大的数的和
我的:
def get_sum(a,b): sum = 0 if a==b: return a if b<a: a, b = b, a for i in range(a,b+1): sum += i return sum
人家的:
def get_sum(a,b): return sum(xrange(min(a,b), max(a,b)+1))
def get_sum(a,b): soma=0 if a==b: return a elif a > b: for i in range(b,a+1): soma += i return soma else: for i in range(a,b+1): soma += i return soma
def get_sum(a,b): lower = min(a, b) upper = max(a, b) + 1 # add 1 to include the last value return sum(range(lower, upper))