1

I'm trying to make a fibonacci number generator that stops at a given amount, but it usually goes past the amount. What am I doing wrong?

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n":
        a=a+b
        b=b+a
        print(a)
        print(b)
else:
    while int(stopNumber) > a or int(stopNumber) > b:
        a=a+b
        b=b+a
        print(a)
        print(b)
4

7 回答 7

1

您获得更高值的原因是因为您拥有a = a+bb = b+a在一个循环中。因此,当您检查while int(stopNumber) > a or int(stopNumber) > b:获取True并进入循环中的值时,a = a+b并且b = b+a可以使 和 的值a大于b并且stopNumber由于您在不检查的情况下打印它,因此您将获得一些更高的值。您应该在循环中只增加一次,如果您在 while 循环之后编写 print 语句,您将不会得到正确的值

prev = 0                             
curr = 1
print("Fibonacci number generator.")
stopNumber = input("How high do you want to go? If you want to go forever, put n.")
if stopNumber == 'n':                    
    print(curr)                     
    curr = prev + curr
    prev = curr
else:
    while curr<stopNumber:
        print(curr)
        curr = prev + curr
        prev = curr

注意:如果输入为 ,代码将永远运行n

于 2013-11-07T14:28:38.170 回答
1

同样,工作并使用更智能的技术:

# returns generator
def fib(stop):
    prev, current = 0, 1
    while current < stop:  # a little hack here - python is ok comparing ints to floats
        yield current
        # multiple assginment - operands on the left are "frozen" just before theis instruction
        prev, current = current, prev + current 

# note inf - float('inf') results in "positive infinity" which is an appropriate math concept for "forever"
stop = float(input("How high do you want to go? If you want to go forever, put inf."))

for f in fib(stop):
    print (f)

注意:请不要尝试这样做list(fib(float('inf'))):)

于 2013-11-07T14:29:44.197 回答
0

您检查 stopNumber > a 或 b,然后增加 a 和 b,打印它们。如果您只想在它们是 <= stopNumber 时打印它们,而不是执行以下操作:

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n":
        a=a+b
        b=b+a
        print(a)
        print(b)
else:
    while True:
        a = a+b
        b = b+a
        if int(stopNumber) >= a:
           print(a)
        if int(stopNumber) >= b:
           print(b)
        else:
          break
于 2013-11-07T14:23:11.223 回答
0

使用您的代码:

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n" or int(stopNumber) > a+b:
    a, b = b, a+b
    print(b)
于 2013-11-07T14:32:25.527 回答
0
 #This is a simple yet efficient program using recursion:

def fibonacci_rec(a, b):
    if a >= 0 and b > 0 or a > 0 and b >= 0:
        s = [a, b]
        while a+b <= 1000: #can set any upper boundary  
            f = a+b
            a = b
            b = f
            s.append(f)
            fibonacci_rec(a, b)
        return s
    else:
        return 'Invalid'

print(fibonacci_rec(1, 1))  # You can set any value of a and b, as you like and no need to iterate in here, just call function once and it does the iteration for you! 
于 2018-01-15T22:31:40.387 回答
0

第二个“while”循环始终保持运行,“a”或“b”低于“stopNumber”。因此,循环继续运行,直到“a”和“b”都大于“stopNumber”。结果,当“b”大于“stopLimit”但“a”仍低于“stopLimit”时,循环继续运行。因此,要应用的第一个解决方法是将“或”条件更改为“和”条件。

您只是在总和之前检查条件是否适用。然后,当总和完成时,它们的结果可能大于“stopLimit”;这就是您打印的内容。要解决此问题,您可以添加“if”语句来验证总和结果是否仍低于“stopNumber”。

以下是这些修复后代码的外观:

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n":
        a=a+b
        b=b+a
        print(a)
        print(b)
else:
    while int(stopNumber) > a and int(stopNumber) > b:
        a=a+b
        b=b+a
        if int(stopNumber) > a:
            print(a)
        if int(stopNumber) > b:
            print(b)
于 2016-02-19T17:34:03.667 回答
-1
def fib(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n-1) + fib(n-2)

print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
if stopNumber == 'n':
    i=1
    while True:
        print 'Fibonacci #{0}: {1}'.format(i, fib(i))
        i=i+1
else:
    for i in range(1,int(n)):
        print 'Fibonacci #{0}: {1}'.format(i, fib(i))
于 2013-11-07T14:23:49.877 回答