我尝试了一些代码,但没有得到令人满意的答案。代码的输出应该是来自调用点的确切数字参数:
>>> 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发布于 2019-04-11 21:11:29
为此,您需要自己检查一下,python不会为您做这件事。
def Hello(PitU, *V):
if len(V) != PitU:
print("error")
return
print("you passed", PitU, "Arguments")
for Pit in V:
print(Pit)发布于 2019-04-11 21:20:03
因为PITu不是您必须传递的参数数量,所以它只是您放在那里的另一个参数。python技术没什么问题,你只是误解了它的概念。
https://stackoverflow.com/questions/55633248
复制相似问题