-3

我是编程和python的新手,我正在看一个讲座,我想创建一个像讲师在视频中所做的简单函数,所以我设计了3个函数,addition,mean,mean_Addition,如下所示,addition只是add 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 中,因为当我删除这部分时它运行良好!

问题是,当我选择 run modulo 时,它说“预期有一个缩进块”

那么我犯的语法错误是什么?

谢谢。


注意:对于那些将来会探索这个问题的人,我犯的语法错误(我从答案中学到了这一点)是我写的

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

但是我们不应该把“'''”放在“def”下面,我们应该在“def”下面留一个空格,所以正确的代码是

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

1 回答 1

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)
于 2013-04-05T23:17:14.813 回答