方法有两种:

方法2比方法1好,因为如果多次变化参数个数的话,方法1意味着需要多次定义列表或者元组,太过麻烦。

1,使用list(列表)或者tuple(元组)

tup1 = (1,2,3,4,5,6)
tup2 = (1,2,3)
def CalcNum1(t):
sum = 0
for n in t:
sum = sum + n * n
return sum

print(CalcNum1(tup1))
print(CalcNum1(tup2))

运行结果:

=================== RESTART: C:/Users/公有制/Desktop/test1.py ===================
91
14
>>>

2,使用*

def CalcNum2(*num):
sum = 0
for n in num:
sum = sum + n * n
return sum

print(CalcNum2(1,2,3,4,5,6))

print(CalcNum2(1,2,3))

运行结果:

=================== RESTART: C:/Users/公有制/Desktop/test1.py ===================
91
14
>>>