gpt4 book ai didi

python - 斐波那契的 `a, b = b, a+b` 和 `a = b; b = a+b` 有什么区别

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:47 24 4
gpt4 key购买 nike

<分区>

我刚接触python所以想问你一个问题..

之前我在写斐波那契函数时尝试替换

a, b = b, a+b

a = b
b = a + b

相信这是同一件事,但我注意到输出不同(而且是错误的)这两个代码不应该做同样的事情吗?这是完整的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def main(args):
fibonacci(1000)
return 0

def fibonacci(n):
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b # if I comment this and decomment the two line below it shows me a different output
# a = b
# b = a + b

if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com