当我在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'
可以通过使用
*
解压缩boolean
列表来传递它。以下是您将如何做到:这样,您就不需要对方法进行任何调整。在
DEMO
或者,您可以让您的
^{pr2}$which_animal
方法接受一个列表,但这段代码本身确实很难理解。在相关问题 更多 >
编程相关推荐