【问题标题】:Fibonacci sequence python斐波那契数列 python
【发布时间】:2013-08-03 07:21:15
【问题描述】:

我正在尝试理解 Python,但我仍然不明白。我是这门语言的新手,想正确理解它。 这是使用循环的斐波那契数列中的一条线。请解释这段代码的含义。我正在尝试手动获取模式。我得到的模式最多为 3,但在 3 之后我没有得到答案。

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

【问题讨论】:

  • 那里没有迭代器。

标签: python loops python-3.x iterator iterable-unpacking


【解决方案1】:
a, b = b, a + b

这称为多重赋值。它基本上是 atomic 版本的:

a = b
b = a + b

通过原子,我的意思是右侧的所有内容都是在将其放入左侧的变量之前计算的。所以a 变成bb 变成a 加上bold 版本,相当于非原子:

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

可以在教程中找到here 的确切代码(以及多重赋值的解释)。

【讨论】:

    【解决方案2】:

    这是多重赋值(或元组解包)。

    根据Python Tutorial

    >>> # Fibonacci series:
    ... # the sum of two elements defines the next
    ... a, b = 0, 1
    >>> while b < 10:
    ...     print(b)
    ...     a, b = b, a+b
    ...
    1
    1
    2
    3
    5
    8
    

    这个例子介绍了几个新特性。

    第一行包含一个多重赋值:变量 a 和 b 同时获取新值 0 和 1。在最后一行是 再次使用,证明右边的表达式 在任何分配发生之前都首先被评估。这 右侧表达式从左到右进行计算。

    【讨论】:

      【解决方案3】:

      多个答案怎么样?

      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)
      

      【讨论】:

        【解决方案4】:

        我知道这是一个老问题,但我只是认为我已经完成了 2 美分,因为其中很多对于斐波那契数列(在给定答案之外)来说似乎有点过于复杂,以防有人还在看着。你可以这样做:

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

        这将给出序列的所需输出(无论您设置的 b 小于什么)。

        【讨论】:

          【解决方案5】:
          #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
          

          【讨论】:

            【解决方案6】:
            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
            

            【讨论】:

              猜你喜欢
              • 2013-02-24
              • 2014-05-23
              • 2015-06-05
              • 2022-01-14
              • 1970-01-01
              • 1970-01-01
              • 2014-05-19
              相关资源
              最近更新 更多