Skip to main content
 首页 » 编程设计

python之根据函数中参数的数量返回不同的值

2025年05月04日22虾米姐

<分区>

我正在尝试创建一个函数,该函数接受 1 到 5 个参数,并根据给定的数字进行不同的计算。 我的想法是这样的:

def function(*args) 
    num_of_args = (!!here is the problem!!) 
if(num_of_args == 1) : result = a 
else if(number_of_args == 2) : result = a+b 

等等 我试图计算参数的数量并将该数字分配给变量但找不到方法 我想可能不需要使用 5 个 if,但在我设法计算这些参数之前我真的不想专注于它