方括号中的函数参数

2024-06-07 15:35:51 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在pythonshell中调用我的函数时,我希望布尔参数的列表既在普通括号中,也在方括号中,比如which-animal([True,True,True,True,True,True,True]),如果有人也能解释所使用的过程,这将非常有帮助。在

这是我的代码供参考

which_animal(backbone,shell,six_legs,give_birth_to_live_babies,feathers,gills,lay_eggs_in_water):

if backbone:
    if give_birth_to_live_babies:
        return 'Mammal'
    if feathers:
        return 'Bird'
    if gills:
        return 'Fish'
    if lay_eggs_in_water:
        return 'Amphibian'
    if not lay_eggs_in_water:
        return 'Reptile'

if not backbone:
    if shell:
        return 'Mollusc'
    if six_legs:
        return 'Insect'
    if not six_legs:
        return 'Arachnid'

Tags: intruewhichreturnifnotshelleggs
1条回答
网友
1楼 · 发布于 2024-06-07 15:35:51

可以通过使用*解压缩boolean列表来传递它。以下是您将如何做到:

which_animal(*[True,True,True,True,True,True,True])

这样,您就不需要对方法进行任何调整。在

DEMO

或者,您可以让您的which_animal方法接受一个列表,但这段代码本身确实很难理解。在

^{pr2}$

相关问题 更多 >

    热门问题