# Filename: fibonaci.py
# author by: stephen
def fib(n): #定义一个函数叫 fib()
if n <= 1: #定义数列的前两个元素为1 1
return n
else:
return(fib(n-1)+fib(n-2)) #根据斐波那契数列的特性第n个元素等于前两个元素之和
nterms = int(input("which element do you want to calculate of fibonacci?"))
if nterms <= 0: #判断如果用户输入的是负数或零就不计算
print("please input positive number!")
else:
print("Fibonaci is : ")
for i in range(nterms):
print i, fib(i)
[Python学习] 斐波那契数列 Fibonacci Sequence
原文地址:http://blog.51cto.com/6150141/2114851