Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-fibonacci生成器的开始_Python_Fibonacci - Fatal编程技术网

Python-fibonacci生成器的开始

Python-fibonacci生成器的开始,python,fibonacci,Python,Fibonacci,我试着做一个斐波那契数生成器,它在给定的数量下停止,但它通常会超过这个数量。我做错了什么 #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

我试着做一个斐波那契数生成器,它在给定的数量下停止,但它通常会超过这个数量。我做错了什么

#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)

如果stopNumber>a或b,则进行检查,然后增加a和b,并打印它们。如果您只想在它们是时打印它们,那么检查stopNumber>a或b,然后增加a和b,打印它们。如果您只想在它们是时打印它们,那么您得到一些更高值的原因是因为您在单个循环中有
a=a+b
b=b+a
。因此,当您检查
中的值,而int(stopNumber)>a或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
prev=0
电流=1
打印(“斐波那契数生成器”)
stopNumber=输入(“你想升多高?如果你想永远升,请输入n.”)
如果stopNumber='n':
印刷(货币)
当前=当前+当前
上一次=当前
其他:

而curr之所以会得到一些更高的值,是因为在单个循环中有
a=a+b
b=b+a
。因此,当您检查
中的值,而int(stopNumber)>a或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
prev=0
电流=1
打印(“斐波那契数生成器”)
stopNumber=输入(“你想升多高?如果你想永远升,请输入n.”)
如果stopNumber='n':
印刷(货币)
当前=当前+当前
上一次=当前
其他:

而curr同样,工作和使用一些更聪明的技术:

# 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)
#返回生成器
def fib(停止):
上一个,当前=0,1
当前

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

同样的事情,使用更聪明的技巧:

# 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)
#返回生成器
def fib(停止):
上一个,当前=0,1
当前
注意:请不要尝试使用您的代码执行
列表(fib(float('inf'))
:)

#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)
使用您的代码:

#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)
第二个“while”循环始终运行,“a”或“b”低于“stopNumber”。因此,循环将继续运行,直到“a”和“b”都大于“stopNumber”。因此,当“b”大于“stopLimit”但“a”仍低于“stopLimit”时,循环将继续运行。因此,应用的第一个修复方法是将“or”条件更改为“and”

您只需在求和之前检查条件是否适用。然后,当求和完成时,其结果可能大于“停止极限”;这就是你打印的内容。要解决这个问题,您可以添加一个“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)
第二个“while”循环始终运行,“a”或“b”低于“stopNumber”。因此,循环将继续运行,直到“a”和“b”都大于“stopNumber”。因此,当“b”大于“stopLimit”但“a”仍低于“stopLimit”时,循环将继续运行。因此,应用的第一个修复方法是将“or”条件更改为“and”

您只需在求和之前检查条件是否适用。然后,当求和完成时,其结果可能大于“停止极限”;这就是你打印的内容。要解决这个问题,您可以添加一个“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)
#这是一个使用递归的简单而高效的程序:
def fibonacci_rec(a,b):
如果a>=0且b>0或a>0且b>=0:
s=[a,b]
a+b
#这是一个使用递归的简单而高效的程序:
def fibonacci_rec(a,b):
如果a>=0且b>0或a>0且b>=0:
s=[a,b]

a+b的时候,你能给我们看一下这个的输出吗?如果你输入20,你会得到什么?非常奇怪的斐波纳契发生器。你为什么要一步一步地做
a=a+b
b=b+a
?它应该是
prev,current=current,prev+current
你能给我们看一下这个的输出吗?如果你输入20,你会得到什么?非常奇怪的斐波那契生成器