【问题标题】:Python code for Fibonacci斐波那契的 Python 代码
【发布时间】:2014-07-09 19:54:07
【问题描述】:

下面的代码可以正常工作,但有人可以解释一下句子后面发生了什么 for i in range(n) 当我为n 输入不同的值时,值如何变化或如何添加。

代码如下:-

def fib():

    old = 0
    new = 1
    new_num = 1 #fibonacci number which will change
                #depending on the value of n.

    n = int(input("Please enter a number: "))
    for i in range(n):
        new_num = old + new
        new = old
        old = new_num
        new_num = new_num

    if n <= 0:
            print ("Please enter a number greater than 0.")
    elif n == 1:
            print ("The value is 1.")
    else:
            print ("The value is %s." % (new_num))

fib()

就像我提到的,我得到了正确的答案,但我无法完全理解代码是如何工作的以及更改值因为“N”没有链接到任何东西。

【问题讨论】:

    标签: loops python-3.x fibonacci


    【解决方案1】:

    听起来你可能习惯于 C 或其他具有类似结构的语言的循环。在 Python 中,for i in range(n): 表示“为0n-1 之间的数字列表中的每个值运行此块内的代码一次,依次将i 设置为每个数字”。 (从技术上讲,在 Python 3 中,它不是一个列表,而是一个迭代器,它按顺序生成数字而不将它们存储在列表中,但在这种情况下,区别在于学术性。)

    所以:

    1. ninput() 函数根据用户输入设置。
    2. n 用作range() 函数的参数,当然这不会改变n 的值(它只是创建一个n 数字列表)。然后,for 循环对列表中的每个数字执行一次。
    3. n 用于确定if/elif/else 语句中的输出,同样不更改n 的值。

    【讨论】:

      【解决方案2】:

      每次运行循环时,您都在用刚刚创建的数字替换旧数字。 n 等于您输入的任何内容。假设您输入了 4。 for 循环将从 0 运行到 3(4 次)。首先,将oldnew 的值相加来创建new_num,因为您有了系列中的下一个数字,然后将oldnew 的旧值覆盖到最后两个值在系列中。它会一直这样做,直到循环结束。

      当 i = 0;旧 = 0;新 = 1; new_num = 1

      当 i = 1;旧 = 1;新 = 1; new_num = 2

      当 i = 2;旧 = 2;新 = 1; new_num = 3

      当 i = 3;旧 = 3;新 = 2; new_num = 5

      【讨论】:

        【解决方案3】:

        循环执行 n 次。每次循环执行时,它都会根据之前的计算值计算第 i 个值。它不会改变 n,只是使用 n 作为它必须评估多少次的指示。

        【讨论】:

          【解决方案4】:

          我为你评论了你的代码。在尝试理解您的代码之前,您应该 100% 知道的是,斐波那契数是两个数的总和,在序列中,在它之前。

          了解这一点很重要,因为“新”和“旧”是占位符。

          **另请注意:** N 链接到某个东西,范围。 N 决定了您要确定的斐波那契范围内的索引。

          def fib():
          
          new = 1
          old = 0
          new_num = 1 #fibonacci number which will change
                      #depending on the value of n.
          
          n = int(input("Please enter a number: "))
          
          for i in range(n):  # This is where the value of N is used 
              new_num = new + old  # new_num is the fibonacchi number at index "i", so to get it add the two before us
              old = new  # Update the old place holder (which is what new was, as we're updating new next)
              new = new_num  # Update new to be what we just calculating, so we can use it on the next iteration
              new_num = new_num  # Does nothing. Remove this line
          
          if n <= 0:
                  print ("Please enter a number greater than 0.")
          elif n == 1:
                  print ("The value is 1.")
          else:
                  print ("The value is %s." % (new_num))
          
          fib()
          

          一如既往,有任何问题,只需在下方添加评论!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-09-22
            • 2014-05-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-03
            • 2013-02-24
            • 1970-01-01
            相关资源
            最近更新 更多