我尝试了一些代码,但没有得到满意的答案。代码的输出应该是来自调用站点的确切数字参数:
>>> 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
最佳答案
为此你需要自己检查,python 不会为你做。
def Hello(PitU, *V):
if len(V) != PitU:
print("error")
return
print("you passed", PitU, "Arguments")
for Pit in V:
print(Pit)
关于python - 单个函数的参数数量可变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55633248/