2、多返回值函数与递归函数
1)多返回值,本质为返回元组
**说明:**程序可返回元组,也可直接返回多个值(系统自动将其封装成元组)
import random
def test():
c1 = chr(random.randint(65, 90))
c2 = chr(random.randint(65, 90))
c3 = chr(random.randint(65, 90))
return c1, c2, c3
r1 = test() # 封装成元组
print(r1, type(r1))
c1, c2, c3 = test() # 元组解包
print(c1, c2, c3)
c1, *c2 = test()
print(c1, c2)
(‘F’, ‘G’, ‘N’) <class ‘tuple’>
U I L
O [‘K’, ‘S’]
2)递归函数
**示例:**计算 n 的阶乘
def frac(n):
if n<=1:
if n==0 or n==1:
return 1
else:
print('n 不能小于0')
else:
return n * frac(n-1)
print(frac(5))
120