简体   繁体   中英

Python - About Fibonacci sequence

I try to make a code that asks from the user to enter a positive number and then tells him/her if this number belongs to the Fibonacci sequence. The problem is that, when I run the code never stops runnning (or eventually I'll get an error). Here's my the code so far:

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):
    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?: ")))
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
    if z == i: return True
    return False
 
print(fibo(input("fibonacci?: ")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM