Python TypeError:不支持的操作数类型/:'NoneType'和'float'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22762078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:36:56  来源:igfitidea点击:

Python TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

pythontypeerror

提问by Zahand

This is a assignment we got from our teacher. We are supposed to use Simpson's Rule to do a numerical integration of a functions f(x) = x*cos(third_root(x))

这是我们老师给我们的作业。我们应该使用辛普森法则对函数进行数值积分f(x) = x*cos(third_root(x))

But we are not allowed to use the built in function of cosor use x**(1.0/3.0)to find the third root.

但是我们不允许使用cosor use的内置函数x**(1.0/3.0)来找到第三根。

I get the errors:

我收到错误:

Traceback (most recent call last):
  File "path", line 104, in <module>
    print simpson(f, 1.0, 50.0, 10)
  File "path", line 91, in simpson
    I += 2 * f(x) + (4.0 * f(x + h))
  File "path", line 101, in f
     return x*final_cos(final_3root(x))
  File "path", line 72, in final_cos
    x = float_mod(x, 2 * pi)
  File "path", line 42, in float_mod
    k = int(x / a)
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

Process finished with exit code 1

And here is my code:

这是我的代码:

import math


def final_3root(a):
    q, m = math.frexp(a)

    if 0.5 > q or q > 1.0:
        raise ValueError('Math domain error')

    x = 0.8968521468804229452995486
    factor_1 = 0.6299605249474365823836053
    factor_2 = 0.7937005259840997373758528

    q_croot = (q / (x * x) + 2.0 * x) / 3.0
    q_croot = (q / (q_croot * q_croot) + 2.0 * q_croot) / 3.0
    q_croot = (q / (q_croot * q_croot) + 2.0 * q_croot) / 3.0
    q_croot = (q / (q_croot * q_croot) + 2.0 * q_croot) / 3.0

    if m % 3.0 == 0.0:
        m /= 3
        answer = math.ldexp(q_croot, m)

    elif m % 3 == 1:
        m += 2
        m /= 3
        answer = factor_1 * math.ldexp(q_croot, m)

    elif m % 3 == 2:
        m += 1
        m /= 3
         answer = factor_2 * math.ldexp(q_croot, m)

    fasit = a ** (1.0 / 3.0)

#----------------------------------------------

def float_mod(x, a):
    k = int(x / a)
    if (x * a) < 0:
        k -= 1
    return x - float(k) * a


def ratio_based_cosinus(x):
    epsilon = 1.0e-16

    previous_Value = 1
    return_Value = 1
    n = -1
    while True:
        n += 1
        ratio = (-x * x) / (((2 * n) + 1) * ((2 * n) + 2))

        previous_Value *= ratio
        return_Value += previous_Value

        if abs(previous_Value) < epsilon:
            break
    return return_Value


def final_cos(x):
    if isinstance(x, int):
        x += 0.0

    pi = 3.1415926

    x = float_mod(x, 2 * pi)

    if x > pi:
        return ratio_based_cosinus(-x)
   else:
        return ratio_based_cosinus(x)

 #----------------------------------------------


def simpson(f, a, b, N):
    if N & 1:
        raise ValueError('Ugyldig tall')

    I = 0
    h = float((b - a) / N)
    x = float(a)

    for i in range(0, N / 2):
        I += 2 * f(x) + (4.0 * f(x + h))
        x += 2 * h

    I += float(f(b) - f(a))
    I *= h / 3

    print "The sum is: ", I


def f(x):


     return x*final_cos(final_3root(x))


print simpson(f, 1.0, 50.0, 10)

采纳答案by Jonathon Reinhart

final_3rootis missing a return statement.

final_3root缺少 return 语句。

Look closely at the error. xis None. If you trace it back, you'll see that the return value of that function is used, but it never returns anything.

仔细查看错误。xNone。如果回溯它,您会看到使用了该函数的返回值,但它从不返回任何内容。