Discuss / Python / 交作业

交作业

Topic source

张康除

#1 Created at ... [Delete] [Delete and Lock User]

请教一下评论区,为啥题目要求返回一个tuple,但是不加括号也能测试成功啊?

    if len(L)==0:
        return None,None
    else:
        max=L[(0)]
        min=L[(0)]
        for x in L:
            if x>max:
                max=x
        for y in L:
            if y<min:
                min=y
        return (min,max)
    if len(L)==0:
        return None,None
    else:
        max=min=L[(0)]
        for i in L:
            if i>max:
                max=i
            if i<min:
                min=i
    return min,max

是因为多个return返回多个数据自动生成为元组吧(不确定)

yeah

python 元组是由逗号决定,而不是小括号

实例:

l = 1,

print(type(l))

#输出结果<class 'tuple'> ,说明l是元组

s = (1)

print(type(s))

#输出结果<class 'int'>,说明s是整数

引申:元组中只包含一个元素时,需要在元素后面添加逗号

tup1 = (50,)

张康除

#5 Created at ... [Delete] [Delete and Lock User]

感谢解答!


  • 1

Reply