-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexample6.py
More file actions
48 lines (40 loc) · 766 Bytes
/
example6.py
File metadata and controls
48 lines (40 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = 'yangdd'
'''
example 006
'''
##
def fib(n):
i,a,b = 0,1,1
while i < n:
yield a
a,b = b,a+b
i+=1
return 'done'
def showFib():
n = int(input("要查看第几位数?"))
g = fib(n)
while True:
try:
x = next(g)
print(x)
except StopIteration as e:
print('Generator return value:', e.value)
break
def recursionFib(n,b1=1,b2=1,c=3):
if n<3:
return 1
else:
if n==c:
return b1+b2
else:
return recursionFib(n,b1=b2,b2=b1+b2,c=c+1)
def showRecursionFib():
n = int(input("要查看第几位数?"))
print(recursionFib(n))
if __name__=='__main__':
# 方法一
# showFib()
# 方法二
showRecursionFib()