WAP 从键盘输入一个整数 N,如果 N 为非负数,则计算并显示从 N 到 (2*N) 的数字之和。如果 N 为负,则

WAP that takes an integer N from keyboard, computes and display the sum of the numbers from N to (2*N) if N is nonnegative. If N is negative then

WAP 从键盘读取整数 N,如果 N 为非负数,则计算并显示从 N 到 (2N) 的数字之和。如果N为负数,则为从(2n)到n的数之和。起点和终点都包含在总和中。

def myfunc(n):
ls=[]

#we are going to append all the number from n to 2n
#and return their sum
#since 2n is also included, therefore end point of loop must be 2n+1

if n<0:
    for i in range(n,2*n-1,-1):
       ls.append(i)
else:
    for i in range (n,2*n+1):
        ls.append(i)
return sum(ls)