Pik*_*ALT 0 python list windows-7-x64 python-3.x
我试图在Python 3.3.2中编写一个简短的函数.这是我的模块:
from math import sqrt
phi = (1 + sqrt(5))/2
phinverse = (1-sqrt(5))/2
def fib(n): # Write Fibonacci numbers up to n using the generating function
list = []
for i in range(0,n):
list = list.append(int(round((phi**i - phinverse**i)/sqrt(5), 0)))
return(list)
def flib(n): # Gives only the nth Fibonacci number
return(int(round((phi**n - phinverse**n)/sqrt(5), 0)))
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Run Code Online (Sandbox Code Playgroud)
当我运行fibo.fib(6)时,我收到以下错误:
list = list.append(int(round((phi**i - phinverse**i)/sqrt(5), 0)))
AttributeError: 'NoneType' object has no attribute 'append'
Run Code Online (Sandbox Code Playgroud)
我该如何纠正这个错误?
返回类型
list.append
Run Code Online (Sandbox Code Playgroud)
是 None
当你这样做 list = list.append(int(round((phi**i - phinverse**i)/sqrt(5), 0)))
它正在分配 list=None
做就是了
for i in range(0,n):
list.append(int(round((phi**i - phinverse**i)/sqrt(5), 0)))
Run Code Online (Sandbox Code Playgroud)
另外,list是内置类型.所以使用不同的变量名称.
| 归档时间: |
|
| 查看次数: |
926 次 |
| 最近记录: |