python中这段代码中的语法错误是什么?

时间:2013-04-05 23:14:21

标签: python code-formatting

我是编程和python的新手,我正在观看演讲,我想创建一个像讲师在视频中所做的简单功能,所以我设计了3个功能,添加,均值,mean_Addition如下图所示,另外只是添加2个数字和平均函数计算两个数字的平均值mean_Addition添加平均值和两个数字的相加

我写了代码然后运行程序,但它告诉我有一些语法错误,我一遍又一遍地检查它但是我无法判断错误是什么

我的简单程序的代码是:

def addition(float1,float2):
     '''(float,float)-> float

return the addition of float1 and float2 .

>>> addition(2,3)
5.0
>>>addition(4,6)
10.0 '''
     return float1+float2


def mean(x , y ):
    '''
(number,number)-> float
return the mean of two numbers , x and y .
>>> mean(2,4)
 3.0
>>> mean(9,2)
5.5
 '''
    return  addition(x,y)/ 2



def Mean_Addition(t,s):
'''
(float,float)->float
return the mean of the two numbers plus the addition of the two numbers
>>> Mean_Addition(1,2)
4.5
>>> Mean_Addition(4,5)
13.5
'''
    return addition(t,s) + mean(t,s)

我要提的一件事是错误是在第三个函数Mean_Addition中,因为当我删除这部分时它运作良好!

问题是,当我选择运行模数时,它会说"期望一个缩进的块"

那么我所做的语法错误是什么?

感谢。


注意:对于那些将来会探讨这个问题的人,我所做的语法错误(我从答案中得知)是我写的

def Mean_Addition(t,s):
'''
(float,float)->float

但是我们不应该把" ''' "在" def" ,我们应该在" def" 所以正确的代码是

def Mean_Addition(t,s):
    '''
(float,float)->float

1 个答案:

答案 0 :(得分:2)

def addition(float1,float2):
     """
     (float,float)-> float
     return the addition of float1 and float2 .
     >>> addition(2,3)
     5.0
     >>>addition(4,6)
     10.0 
     """
     return float1+float2


def mean(x , y ):
    """
    (number,number)-> float
    return the mean of two numbers , x and y .
    >>> mean(2,4)
    3.0
    >>> mean(9,2)
    5.5
    """
    return  addition(x,y)/ 2



def Mean_Addition(t,s):
    """ <----- the error was here
    (float,float)->float
    return the mean of the two numbers plus the addition of the two numbers
    >>> Mean_Addition(1,2)
    4.5
    >>> Mean_Addition(4,5)
    13.5
    """
    return addition(t,s) + mean(t,s)