我尝试了一些代码,但没有得到满意的答案。代码的输出应该是呼叫站点的确切数字参数:
>>> def Hello(PitU,*V):
print("you passed" , PitU,"Arguments")
for Pit in V:
print(Pit)
#case1
>>> Hello(3,"one","two","three")
you passed 3 Arguments
one
two
three
#case2
>>> Hello(3,"one","two")
you passed 3 Arguments
one
two
#case3
>>> Hello(3,"one","two","three","four")
you passed 3 Arguments
one
two
three
four
>>>
我希望输出为:
A. case-1
you passed 3 Arguments
one
two
three
B. case-2
error
C. case-3
error
instead of
Case1
you passed 3 Arguments
one
two
three
case2
you passed 3 Arguments
one
two
case3
you passed 3 Arguments
one
two
three
four
答案 0 :(得分:3)
为此,您需要自己进行检查,python不会帮您这样做。
def Hello(PitU, *V):
if len(V) != PitU:
print("error")
return
print("you passed", PitU, "Arguments")
for Pit in V:
print(Pit)
答案 1 :(得分:3)
因为PITu不是您必须传递的参数数目,所以它只是您输入的另一个参数。 python技术没有错,您只是误解了其概念。