接收n个数字,求这些参数数字的和。
函数定义:
sum_func(*args)
提示:args为可变参数,其可接受若干个实参值,并将接收到的值组装为一个元组。
裁判测试程序样例:
/* 请在这里填写答案 */
result = sum_func(1, 2, 3, 7, 4, 5, 6)
print(result)
输入样例:
无
输出样例:
在这里给出相应的输出。
28
我的答案:
简单写法:
def sum_func(*args):
return sum(args)
复杂写法:
def sum_func(*args):
total = 0
for i in args:
total += i
return total
测试结果:
两种写法均正确