一步一步解释这段代码 [英] Step by Step explanation of this code

查看:137
本文介绍了一步一步解释这段代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案 def mystery(n):#定义一个名为mystery的函数,接受一个名为n的参数
a,b = 0,1#将一个名为a的变量设置为0;制作一个名为b的变量并将其设置为1
,而< n:#只要a小于n
print(a)#在屏幕上显示a中包含的值
a,b = b,a + b#set b与a和b的总和;将a设置为b的旧值(在它被设置为a和b之前)


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

If someone could give me a line by line explanation of this code, as also inform of why it will not run, and what extra code needs to be added.

解决方案

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

这篇关于一步一步解释这段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆