Python-关于Fibonacci序列



我尝试编写一个代码,要求用户输入一个正数,然后告诉他/她这个数字是否属于斐波那契序列。问题是,当我运行代码时,它永远不会停止运行(或者最终会出现错误(。这是我迄今为止的代码:

print("nEnter any positive number, to see if it")
user = input("belongs to the Fibonacci sequence: ")
def fibo(user):
if user in [0,1]:
return user
else:
return fibo(user-1) + fibo(user-2)

while user.isdigit() == False:
user = input("Input error. Please enter a positive number: ")
else:
user = int(user)
if user == fibo(user):
print("nNumber",user,"belongs to the Fibonacci sequence.n")
else:
print("nNumber",user,"doesn't belong to the Fibonacci sequence.n")
def fibo(input):
i = int(input)
# set z to 1 if you want
# False to be returned for 0
x = 0; y = 1; z = 0
while z < i:
z = x + y; x = y; y = z
return z == i

print(fibo(input("fibonacci?: ")))
def fibo(input):
if int(input) == 0: return [0]
x = 0; y = 1; z = 0; erg = []
while True:
z = x + z; x = y; y = z
if y > int(input): break
else: erg.append(y)
if int(input) in erg: return True
else: return False

print(fibo(input("fibonacci?: ")))

最新更新