递归
#https://mp.weixin.qq.com/s/8BwlD7GBvBbCDcSEZnI0_w
print(“——1. 循环——–”)
a = 1
while a < 10:
print(a)
a += 2
print(“——–2. 带 if/else 的循环————–”)
a = 1
while a < 10:
if a % 2 ==0:
print(a,“是偶数”)
else:
print(a,“是奇数”)
a += 1
print(“——————3. 递归的代码示例———————-”)
line =[1,2,3,4,5,6]
def howmany(line):
if line[1:]:
print(“me and the guys behind”)
return 1 + howmany(line[1:])
else:
print(“just me”)
return 1
howmany(line)
print(“————————–4. 递归求斐波那契数列———————–”)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
print(n)
return fib(n-1) + fib(n-2)
fib(7)
print(“———————–5. 递归求阶乘———————-”)
def factorial(n):
if n == 1:
return 1
else:
print(“阶乘”,n)
return n * factorial(n-1)
factorial(4)
厉害
回帖内容已被屏蔽。