def calc(*numbers):

    #定义一个int类型的常量,用以接收结果

    sum = 0

    #定义一个空集合,用已接收数组中每一个数字的次方

    l = []

    for n in numbers:

        sum =  n * n

        #l = l.append(sum)不能这样写,报错

        #'NoneType' object has no attribute 'append'

        #因为append会修改l本身,并返回None。不能把返回值在赋值给l

        #把得到的结果依次放入集合中

        l.append(sum)

    return l

#注:calc中赋值时只能是int类型,不要加中括号,会把类型变成集合并报错

print(calc(1,2,3,3,65,47,4,756,756,75,63543,45))

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐