>>> def powersum(power, *args):
... '''Return the sum of each argument raised to specified power.'''
... total = 0
... for i in args:
... total += pow(i, power)
... return total
...
>>> powersum(2, 3, 4)
25
>>> powersum(2, 10)
100
解析:
函数定义时只有一个行参power,其实参将按位置匹配原则传值
args是用来存储多余的参数,即按位置匹配不上的参数
... '''Return the sum of each argument raised to specified power.'''
... total = 0
... for i in args:
... total += pow(i, power)
... return total
...
>>> powersum(2, 3, 4)
25
>>> powersum(2, 10)
100
解析:
>>> def powersum(power, *args):
... '''Return the sum of each argument raised to specified power.'''
函数定义时只有一个行参power,其实参将按位置匹配原则传值
args是用来存储多余的参数,即按位置匹配不上的参数