【发布时间】:2019-10-27 04:39:00
【问题描述】:
我尝试在 python 中生成斐波那契,但我注意到,如果我通过交换来生成它,它会给我一个不同的值,而不是我通过简单赋值来生成它
def fib_num(max):
a = 0
b = 1
for i in range(max):
# a,b = b+a,a this way it is right
# but if I will implement it like below with simple assigment,
# I am not going to get the same result Why???
a = b+a
b = a
yield a
【问题讨论】:
-
因为在第二个示例中,您已经更改了
a的值,之前然后将该新值分配给b。 -
temp = a a = a+b b = temp 就像 a, b = a+b, a
标签: python python-3.x