#python中有两种多值参数:# (1)参数名前加一个*可以接收元组(2)参数名前增加两个 *可以接收字典。#使用多值参数求和defsum_numbers(*args):
num=0print(args)for n in args:
num+=n
return num
result=sum_numbers(1,2,3,4,5)print(result)#使用元组参数求和defsum(agrs):
num=0print(agrs)for n in agrs:
num+=n
return num
resulta=sum((1,2,3,4,5))#需要多加一个括号,不如多值参数方便print(resulta)
07递归累计求和
#定义一个函数sum_numbers#能够接收一个num的整数参数#计算1+2+3...num的结果defsum_numbers(num):#出口if num ==1:return1#假设sum_numbers能够正确处理累加,再设计累加
temp=sum_numbers(num-1)return num +temp
result=sum_numbers(3)print(result)