新的python.现在无法找出这个问题的答案2天..会感激一些帮助,谷歌搜索没有帮助.
填写"foo"和"bar"函数,以便它们可以接收可变数量的参数(3或更多)"foo"函数必须返回所接收的额外参数的数量.如果带有关键字"magicnumber"的参数值为7,则"bar"必须返回"True",否则返回False.
# edit the functions prototype and implementation
def foo(a, b, c):
pass
def bar(a, b, c):
pass
# test code
if foo(1,2,3,4) == 1:
print "Good."
if foo(1,2,3,4,5) == 2:
print "Better."
if bar(1,2,3,magicnumber = 6) == False:
print "Great."
if bar(1,2,3,magicnumber = 7) == True:
print "Awesome!"
Run Code Online (Sandbox Code Playgroud)
我猜..一些部分代码会很好,无法理解**kwargs以及所有这些:
我不确定你是否只想要有人给你代码,但既然你说你正在努力学习,我现在就指出你正确的方向.您想使用Python的关键字参数.
[编辑]
这是代码:
def foo(a, b, c, *args):
return len(args)
def bar(a, b, c, **kwargs):
if kwargs["magicnumber"] == 7:
return True
return False
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7970 次 |
| 最近记录: |