jackfrued / Python-Core-50-Courses

Python语言基础50课

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

python-core-50,第20课发现错误

ksi1110 opened this issue · comments

def calc(*args, **kwargs):
result = 0
for arg in args:
result += arg
for value in kwargs.values():
result += value
### return total

最后应该是return result

函数使用进阶,高阶函数,发现代码位置错误
import operator

print(calc(init_value=0, op=operator.add, 1, 2, 3, x=4, y=5)) # 15
print(calc(init_value=1, op=operator.mul, 1, 2, x=3, y=4, z=5)) # 120

位置参数应该写在关键字参数前面,正确代码为:
print(calc(1, 2, 3, init_value=0, op=operator.add, x=4, y=5))
或者
print(calc(1, 2, x=3, y=4, z=5, init_value=1, op=operator.mul))

commented

i also find out this mistake,please change it