我正在使用以下代码:
import csv
f = csv.writer(open("pe_ratio.csv","w"))
def factorial(n):
results = 1
# results are one because that is the minimum number
while n >= 1:
results = results * n
# results * nth number of n
n = n - 1
# n - 1
# two variables are affected in this program results and n
return f.writerow([results])
print factorial(4)
file.close()
我得到这个错误:
Traceback (most recent call last):
File "pe_ratio.py", line 23, in <module>
print factorial(4)
File "pe_ratio.py", line 21, in factorial
return f.writerows([results])
_csv.Error: sequence expected
我怀疑我需要阅读CSV 文档并深入了解这个问题。我想要完成的是让 csv 文件写入连续阶乘(4)的结果。我已经尝试取出 factorial(4) 的打印功能并且程序停止了。我在这里先向您的帮助表示感谢。