斐波那契数列使用PYTHON中的清单? [英] Fibonacci sequence using list in PYTHON?

查看:65
本文介绍了斐波那契数列使用PYTHON中的清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将斐波那契序列制作为列表时遇到问题,我是python的新手,请有人帮助我.

I have a problem about making a fibonacci sequence to a list, I'm just new to python someone help me please.

这是我的代码.我知道这看起来不对劲,因为它显示了无效的语法.我真的不知道该怎么办:(

This is my code. I know this is looking wrong or something because it says invalid syntax. I don't know what to do about this really :(

此代码适用于常规代码,无需使用列表!

This code works for a normal code without using a list!

myArray1 = [0] 
myArray2 = [1]

while myArray2 < 700:
    myArray1, myArray2 = b[i], myArray1+myArray2[i]
    print(myArray2)

推荐答案

此代码将前700个斐波那契数字放在列表中.使用有意义的变量名有助于提高可读性!

This code puts the first 700 fibonacci numbers in a list. Using meaningful variable names helps improve readability!

fibonacci_numbers = [0, 1]
for i in range(2,700):
    fibonacci_numbers.append(fibonacci_numbers[i-1]+fibonacci_numbers[i-2])

注意:如果您使用的是Python< 3,使用xrange代替range.

Note: If you're using Python < 3, use xrange instead of range.

这篇关于斐波那契数列使用PYTHON中的清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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