【发布时间】:2015-08-22 03:26:42
【问题描述】:
我正在使用 Python 2.7.6(默认,2015 年 6 月 22 日,17:58:13)编写一个小型 Python 模块,并且我正在使用 IPython 1.2.1 来运行我的程序。
该模块将包含几个具有幂级数的数学函数评估。我将 python 函数定义与 python 函数测试分开。
这是我的 PowerSeries.py 代码:
### Computations of some functions by power series ###
def exp(x):
"""Exponential function"""
exp=1.0
term=1.0
iteration=1
factorial=1
while(abs(term/factorial)>1e-10):
factorial*=iteration
term*=x
exp+=term/factorial
iteration+=1
return exp
def hyperbolicCosine(x):
"""Hyperbolic cosine function"""
cosh=1.0
term=1.0
iteration=1
factorial=1
while(abs(term/factorial)>1e-10):
factorial*=iteration
term*=x
if iteration%2==0:
cosh+=term/factorial
iteration+=1
return cosh
def sine(x):
"""Sine function"""
sine=0.0
term=1.0
iteration=1
factorial=1
while(abs(term/factorial)>1e-10):
factorial*=iteration
term*=x
if iteration%2==1:
sine+=term/factorial
term=-term
iteration+=1
return sine
def cosine(x):
"""Cosine function"""
cosine=1.0
term=1.0
iteration=1
factorial=1
while(abs(term/factorial)>1e-10):
factorial*=iteration
term*=x
if (iteration)%2==0:
term=-term
cosine+=term/factorial
iteration+=1
return cosine
这是我的 tests.py 代码:
### Tests for PowerSeries Module ###
from math import pi
from PowerSeries import *
# Tests of exponential
print "### Tests of exponential ###"
x=-2
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=-1
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=0
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=1
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=2
print("x=%f exp(x)=%.10f" % (x,exp(x)))
# Tests of hyperbolic cosine
print "### Tests of hyperbolic cosine ###"
x=-2
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=-1
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=0
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=1
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=2
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
# Tests of sine
print "### Tests of sine ###"
x=0
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/6
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/4
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/3
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/2
print("x=%f sin(x)=%.10f" % (x,sine(x)))
# Tests of cosine
print "### Tests of cosine ###"
x=0
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/6
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/4
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/3
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/2
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
在我添加双曲余弦定义之前,一切正常。 执行 tests.py 文件会出现我无法解决的 NameError :
19 print "### Tests of hyperbolic cosine ###"
20 x=-2
---> 21 print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
22 x=-1
23 print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
NameError: name 'hyperbolicCosine' is not defined
欢迎帮助。
【问题讨论】:
-
你确定你没有一个旧的 .pyc 文件,它的日期比你的 .py 文件晚,或者一个 .py 文件与你认为的目录不同吗使用?暂时重命名 PowerSeries.py 并以不同的名称导入。
-
你如何运行
tests.py代码,你能显示准确的逗号吗?是否来自 ipython 解释器? -
好吧,我使用命令 run tests.py 从 ipython 解释器中运行 tests.py 代码。但根据 Patrick Maupin 的说法,我实际上是通过删除旧的 PowerSeries.pyc 文件解决了这个错误。所以,它现在可以工作了,我将添加更多的函数定义来考虑这个问题的发生。谢谢你们。