小编Moo*_*Moo的帖子

为什么这两个python函数会返回不同的结果?

1-

 def fib1(n):
     a = 0
     b = 1
     while a < n:
        print b
        a = b
        b = a+b
Run Code Online (Sandbox Code Playgroud)

2-

 def fib2(n):
     a, b = 0,1
     while a < n:
         print b
         a,b = b, b+a
Run Code Online (Sandbox Code Playgroud)

执行时:

fib1(10) 我得到了错误的答案: 0 1 2 4 8

fib2(10) 我得到了正确的答案: 0 1 1 2 3 5 8

python python-2.7 python-3.x

3
推荐指数
1
解决办法
99
查看次数

标签 统计

python ×1

python-2.7 ×1

python-3.x ×1