在python3.4中打印最大最小的

2024-04-19 06:44:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python,这是我喜欢的Udacity课程,“Python介绍计算机科学”。我的尝试是:

def biggest(x,y,z):
    max = x
    if y>max:
        max = y
    if  z>max:
        max = z
    return max

def smallest(x,y,z):
    min = x
    if y<min:
        min = y
        if z<min:
            min = z
    return min

def set_range(x,y,z):
    result==biggest-smallest
    return result


print set_range(10, 4, 7)

我收到一条错误消息:

^{pr2}$

为什么我得到这个错误?


Tags: returnifdef错误rangeresultminmax
2条回答

这里有一些更正

def biggest(x,y,z):
    max = x
    if y>max:
        max = y
    if  z>max:
        max = z
    return max

def smallest(x,y,z):
    min = x
    if y<min:
        min = y
        if z<min:
            min = z
    return min

def set_range(x,y,z):
    big = biggest(x,y,z) #assign the function result to a variable
    small = smallest(x,y,z) #also let it inherit the inputs
    result = big - small
    print big
    print small
    return result


print set_range(10, 4, 7)

我不知道为什么你有两次相同的函数,但你需要实际调用这些函数,传递参数,并使用=来分配非==来表示相等:

def biggest(x, y, z):
    mx = x 
    if y > mx:
        mx = y
    if z > mx:
        mx = z
    return mx    

def smallest(x, y, z):
    mn = x
    if y < mn:
        mn = y
    if z < mn:
        mn = z
    return mn


def set_range(x, y, z):
    # use  "=" for assignment not  "==" for equality
    result = biggest(x, y, z) - smallest(x, y, z)
    return result

print set_range(10, 4, 7)

==用于测试两个值是否相等,即1 == 1,单个=用于为变量指定名称,即foo = 1。在

另外最好避免隐藏内置的max和min函数,所以我更改了函数的名称。在

相关问题 更多 >