【问题标题】:Adding or subtracting functions results in none type error添加或减去函数会导致无类型错误
【发布时间】:2021-06-02 14:05:11
【问题描述】:

我正在做一个课堂作业,我们必须使用给定的函数并找出它与 math.sqrt 模块之间的区别。每当我尝试添加或减去任何函数(我使用不同的示例来尝试自己解决问题)时,我都会收到一个错误:TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType' (I我正在使用 PythonAnywhere)。

这就是我正在做的事情:

    import math
    def my_sqrt(a):
        x = a
        while True:
            print(x)
            y = (x + a/x) / 2
            if y == x:
                break
            x = y
    def test_sqrt(a):
        b = my_sqrt(a)
        c = math.sqrt(a)
        d = abs(b-c)
        print(d)

【问题讨论】:

  • 你的函数没有return 任何东西。当您执行b = my_sqrt(a) 时,您希望b 发生什么,这是如何发生的?

标签: python typeerror


【解决方案1】:

你需要在my_sqrt 函数中返回一些东西。它目前正在返回None

【讨论】:

  • 这应该是评论,而不是答案。
  • 它本来是要返回“x”,而我只是错过了那部分说明!谢谢!
猜你喜欢
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多