Variable assignment in expressions

后端 未结 4 1909
傲寒
傲寒 2021-01-16 13:36

Here is my code to generate values in the fibonnacci sequence below 10,000,000.

  3 fibs = [1,1]
  4 while((x = fibs[-1] + fibs[-2]) <= 10000000):
  5             


        
4条回答
  •  無奈伤痛
    2021-01-16 14:34

    In Python, assignment is not an expression, and therefore has no value.

    The simplest solution is to do the assignment in the first part of the loop:

    fibs=[1,1]
    while fibs[-1] <= 10000000:
       fibs.append(fibs[-1] + fibs[-2])
    

提交回复
热议问题