Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 斐波那契序列_Python_Loops_Python 3.x_Iterator_Iterable Unpacking - Fatal编程技术网

Python 斐波那契序列

Python 斐波那契序列,python,loops,python-3.x,iterator,iterable-unpacking,Python,Loops,Python 3.x,Iterator,Iterable Unpacking,我试图理解Python,但我还是不懂。我对这门语言不熟悉,想正确地理解它。 这是使用循环的斐波那契序列的一条线。请解释此代码的含义。我正试着用手拿图案。我得到了3的模式,但3之后我没有得到答案 a, b = 0, 1 while b < 50: print(b) a, b = b, a + b a,b=0,1 b>>b

我试图理解Python,但我还是不懂。我对这门语言不熟悉,想正确地理解它。 这是使用循环的斐波那契序列的一条线。请解释此代码的含义。我正试着用手拿图案。我得到了3的模式,但3之后我没有得到答案

a, b = 0, 1
while b < 50:
    print(b)
    a, b = b, a + b
a,b=0,1
b<50时:
印刷品(b)
a、 b=b,a+b
它是多重分配(或元组解包)

根据:

斐波那契数列: ... # 两个元素之和定义下一个元素 ... a、 b=0,1 >>>b<10时: ... 印刷品(b) ... a、 b=b,a+b ... 1. 1. 2. 3. 5. 8. 本例介绍了几个新特性

第一行包含一个多重赋值:变量a和b 同时获取新值0和1。最后一行是 再次使用,表明右侧的表达式 在进行任何作业之前,首先对所有作业进行评估。这个 右侧表达式从左到右求值

这称为多重分配。它基本上是原子版本的:

a = b
b = a + b
所谓原子,我的意思是,右边的所有内容都是在调整到左边的变量之前计算出来的。因此
a
变成
b
b
变成
a
b
的旧版本,相当于非原子:

old_a = a
a = b
b = old_a + b
所以,就你所看到的而言:

        a                        b               output
================    =========================    ======
(initial values)        (initial values)
        0                        1                  1
(becomes prev b)    (becomes sum of prev a,b)
        1                        1                  1
        1                        2                  2
        2                        3                  3
        3                        5                  5
        5                        8                  8

具体代码(以及多个作业的解释)可以在教程中找到。

多个答案如何

def fib(num):
    a = 0
    b = 1
    while b <= num:
        prev_a = a
        a = b
        b = prev_a +b
        #print b                                                                                                          
    return a

print fib(13)

def pythonic_fib(num):
    a,b = 0,1
    while b <= num:
        a,b = b, a+b
    return a

print pythonic_fib(13)

def recursive_fib(num, a, b):
    if (b >= num):
        return b
    else:
        return recursive_fib(num, b, a+b)

print recursive_fib(13, 0, 1)
def fib(num):
a=0
b=1

虽然b我知道这是一个老问题,但我想我已经用2美分解决了,因为对于斐波那契序列(在给定的答案之外),很多问题似乎有点过于复杂,以防有人还在看。您可以这样做:

a=1
b=0
while b<400:
    a=a+b
    b=a+b
    print(a)
    print(b)
a=1
b=0

B这里没有迭代器。
def fib(num):
    a = 0
    b = 1
    while b <= num:
        prev_a = a
        a = b
        b = prev_a +b
        #print b                                                                                                          
    return a

print fib(13)

def pythonic_fib(num):
    a,b = 0,1
    while b <= num:
        a,b = b, a+b
    return a

print pythonic_fib(13)

def recursive_fib(num, a, b):
    if (b >= num):
        return b
    else:
        return recursive_fib(num, b, a+b)

print recursive_fib(13, 0, 1)
#The easy way to generate Fibonacci series in python is 
user = input('Please enter the integer range for Fibonacci series: '); # take input from user form the range of Fibonacci series.
try:# to ignore wrong inputs and be aware from error we use try and except block
    d=int(user);# converts the user input to type integer.
    a=0; #initialization``
    b=1; #initialization
    print(a,b,end=' '); # print initial value of a and b
    for c in range(0,d): # here c is iterable variable and in range o is the starting point and d is the ending range which we get from user
        c=a+b;
        a=b;
        b=c;
        print(c,end=' ');

except Exception as e:
    print(e); # if any error occurred in the input here it shows which error occurs
a= int(input("Enter the first number:"));
Enter the first number:0
b= int(input("enter the second number:"));
enter the second number:1
n= int (input("enter the number of terms:"));
enter the number of terms:10
print(a,b);
0 1
while(n>2):
      c=a+b;
      a=b;
      b=c;
      print(c);
      n=n-1;


1
2
3
5
8
13
21
34
a=1
b=0
while b<400:
    a=a+b
    b=a+b
    print(a)
    print(b)