def sum_func(*args):
sm = 0
for i in args:
sm += i
return sm
print(sum_func(1,2,3,7,4,5,6))
以上程序执行结果为
sm = 0
for i in args:
sm += i
return sm
print(sum_func(1,2,3,7,4,5,6))
以上程序执行结果为
举一反三
- def func(): global sum sum=0 print(sum) for i in range(5): sum+=1 print(sum) func() print(sum)
- 自定义函数,计算不定长参数累加和。 (1) (2)((3)): (4) for i in args: s(5)i print(s) sum(1,2,3,4,5,6,7,8,9)
- 下列程序的输出结果是:sum = 0 def sum(i1, i2): result = 0 for i in range(i1, i2 + 1): result += i return result print sum(1, 10)
- ls = ['I','LOVE'] def func(a): ls = [] ls.append(a) return func('WTU') print(ls)
- 请阅读下面的程序,sum的值为()publicclassTest{publicstaticvoidmain(String[]args){intsum=0;for(inti=1;i=100;i++){if(i%2==1){continue;}sum+=i;}System.out.println(sum=+sum);}}