逐步解释此代码

时间:2015-03-09 19:02:13

标签: python fibonacci function

def mystery(n):
   a, b = 0, 1
   while a < n:
      print (a)
      a, b = b, a + b

如果有人可以给我一行代码解释这个代码,也告诉我为什么它不会运行,以及需要添加什么额外的代码。

1 个答案:

答案 0 :(得分:3)

def mystery(n):  # define a function named "mystery", that takes one argument called "n"
    a, b = 0, 1  # make a variable named "a" and set it to 0; make a variable named "b" and set it to 1
    while a < n:  # as long as "a" is smaller than "n"
       print (a)  # display the value contained in "a" on the screen
       a, b = b, a + b  # set "b" to the sum of "a" and "b"; set "a" to the old value of "b" (before it was set to the sum of "a" and "b")
相关问题