python交互环境中导入文件中自定义的函数报错

今天在学习python自定义函数时,遇到一个问题:我用notepad++编辑器自定义的函数,在交互环境下使用from 文件名 import 函数名 导入时,一直报错,检查了好几遍,一直报这个错:

代码如下:

# -*-coding:utf-8 -*-
#自定义函数 def 函数名(参数1,参数2...): 然后在缩进体内编写函数体,用return返回值
#自定义求绝对值函数
#def my_abs(x):
    #如果参数类型不是int或者float,会抛出类型错误异常
#    if not isinstance(x,(int,float)):
#        raise TypeError('bad operand type')
#    if x > 0 :
#        return x
#    else :
#        return -x

#print(my_abs(-2))

import math

def quadratic(a,b,c):
    if (not isinstance(a,(int,float))) or (not isinstance(b,(int,float))) or (not isinstance(c,(int,float))):
        raise TypeError("bad operand type")
    x = (b*b)/(4*a*a)-c/a
    y = math.sqrt(x)-b/(2*a)
    return y

最后发现再导入的时候没有生成编译文件,把之前的编译文件删除之后再重新导入下就可以了

 

posted on 2018-08-26 22:37  永不宕机  阅读(500)  评论(0编辑  收藏  举报

导航