您的位置:首页 > 编程语言 > Python开发

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

2017-06-13 11:44 253 查看
#!/usr/bin/python

def total(initial=5,*numbers,vegetables):
count = initial
for number in numbers:
count += number
count += vegetables
return count
print(total(10,1,2,3,vegetables=50))
print(total(10,1,2,30))


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

第二次调用就出现问题:

66
Traceback (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必须给一个值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: