首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单个函数的参数数量可变

单个函数的参数数量可变
EN

Stack Overflow用户
提问于 2019-04-11 21:08:02
回答 2查看 59关注 0票数 1

我尝试了一些代码,但没有得到令人满意的答案。代码的输出应该是来自调用点的确切数字参数:

代码语言:javascript
复制
>>> 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
>>> 

我希望输出是:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2019-04-11 21:11:29

为此,您需要自己检查一下,python不会为您做这件事。

代码语言:javascript
复制
def Hello(PitU, *V): 
    if len(V) != PitU:
        print("error")
        return
    print("you passed", PitU, "Arguments") 
    for Pit in V: 
        print(Pit)
票数 3
EN

Stack Overflow用户

发布于 2019-04-11 21:20:03

因为PITu不是您必须传递的参数数量,所以它只是您放在那里的另一个参数。python技术没什么问题,你只是误解了它的概念。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55633248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档