【问题标题】:ImportError: No module named errorImportError:没有名为错误的模块
【发布时间】:2018-03-02 06:17:01
【问题描述】:

在 Python 工程中的数值方法,第 2 版,作者:Jaan Kiusalaas,我编写了与第 146 页相同的模块,该模块使用二分法计算根 f(x) = 0:

from math import log,ceil
import error

def bisection2(f,x1,x2,switch=0,tol=1.0e-9):
        f1 = f(x1)
        if f1 == 0.0: return x1
        f2 = f(x2)
        if f2 == 0.0: return x2
        if f1*f2 > 0.0: error.err('Root is not bracketed')
        n = ceil(log(abs(x2 - x1)/epsilon)/log(2.0))
        for i in range(n):
            x3 = 0.5*(x1 + x2); f3 = f(x3)
            if (switch == 1) and (abs(f3) > abs(f1)) \
                                         (abs(f3) > abs(f2)):
                return None
            if f3 == 0.0:return x3
            if f2*f3 < 0.0:
                x1 = x3; f1 = f3
            else:
                x2 = x3; f2 = f3
        return (x1 + x2)/2.0

我遇到以下错误:

ImportError Traceback(最近一次调用最后一次) /usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc 在 execfile(fname, *where) 173 其他: 第174章 --> 175 内置.execfile(文件名, *where)

/home/uwhpsc/Desktop/bisection2/bisection2.py in () 1 来自数学导入日志,ceil ----> 2 导入错误 3 4 def bisection2(f,x1,x2,switch=0,tol=1.0e-9): 5 f1 = f(x1)

ImportError: 没有名为错误的模块

谁能告诉我如何解决这个问题?

【问题讨论】:

  • 还有一段代码打算放在另一个文件中,可能叫做error.py,你需要“写”

标签: python python-2.7 numerical-methods bisection


【解决方案1】:

要使此代码正常工作,您可以删除以下行:

import error

在上面写着:

if f1*f2 > 0.0: error.err('Root is not bracketed')

将其更改为:

if f1*f2 > 0.0: quit('Root is not bracketed')

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 2019-07-31
    • 1970-01-01
    • 2019-07-30
    • 2018-12-08
    • 2014-02-08
    • 2012-05-11
    • 2013-04-06
    • 1970-01-01
    相关资源
    最近更新 更多