我的代码有问题:错误"没有类型"

时间:2018-02-16 20:26:53

标签: python python-3.x syntax-error

  1. 函数接受两个函数f和g,并找到a和b之间的交点与eps的容差,换句话说,找到点c使得f(c)的绝对值 - g(c)
  2. 上面的问题是我得到的"没有"作为我的输出

    #defining two functions
    
    f1 = lambda x: 2*x + 3
    g1 = lambda x: 3*x - 4
    
    # making a function called "intersect"
    # a and b is the upper and lower limit
    # eps is the tolerance of the answer
    def intersect(f,g,a,b,eps):
        c1= 0                   #defining c1 (real number) for simplicity                   
        n = 1                   # to limit the number of iteration
       while n <= 1000:
           c1 = (a+b)/2        #using bisection formula
           if abs(f(c1) - g(c1)) < eps or ((b-a)/2)<eps:       # two conditions |f(c)-g(c)| <eps
               break
               return c1
           if f(c1)<=0:        # accd to bisection theorem
               a = c1
           else:
               b = c1         # accd to bisection theorem
           n +=1              # incrementing to limit the iteration
    print(intersect(f1,g1, -10, 10, 0.001))
    

1 个答案:

答案 0 :(得分:1)

问题在于:

   if abs(f(c1)-g(c1)) < eps or ((b-a)/2) < eps:
       break
       return c1

break语句在返回值之前退出while循环,并且由于没有返回其他值,函数的返回值为None
返回值后,函数退出,因此您不需要break语句。