python第16篇之关键参数赋值(keyword)

来源:互联网 发布:python的应用范围 编辑:程序博客网 时间:2024/05/18 16:16
#!/usr/bin/pythondef total(initial=5,*numbers,vegetables):    count = initial    for number in numbers:        count += number    count += vegetables    return countprint(total(10,1,2,3,vegetables=50))print(total(10,1,2,30))


定义一个函数,调用了两次,第一次没有问题。

第二次调用就出现问题:

66Traceback (most recent call last):  File "./9keywords.py", line 10, in <module>    print(total(10,1,2,30))TypeError: total() missing 1 required keyword-only argument: 'vegetables'

原因时vegetables这个参数没有给值。这里的vegetables是一个key-word必须给一个值。

原创粉丝点击