不支持的操作数类型

时间:2014-06-14 23:30:34

标签: python

def  double(n1):
    print  2 * n1

def triple(n):
   print  3 * n

def add(a,b):
   print double(a) + triple(b)

double(4)
triple(5)
add(3,5)

但是我得到了下面提到的错误,帮帮我

8
15
6
15

Traceback (most recent call last):

  File "python", line 35, in <module>

  File "python", line 28, in add

TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

2 个答案:

答案 0 :(得分:4)

您没有从方法doubletriple返回。因此这个问题。

如果未返回任何内容,则默认情况下该方法返回None

将您的定义更改为:

def  double(n1):
    #print  2 * n1
    return 2 * n1

def triple(n):
   #print  3 * n
   return  3 * n

下面,

def add(a,b):
   print double(a) + triple(b)

没关系,除非你在进一步处理中使用返回值。我会让你自己弄清楚所有其他错误检查(返回值doubletriple等。)

答案 1 :(得分:0)

你不会返回任何内容,因此None。只需将print更改为return s:

即可
def  double(n1):
   return  2 * n1

def triple(n):
   return  3 * n

def add(a,b):
   return double(a) + triple(b)

double(4)
triple(5)
add(3,5)
相关问题