公共和儿媳

Discuss / Python / 参数的使用

参数的使用

Topic source

露心

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

#参数的使用

def mul(x, *args):

    for i in args:

        x = x * i

    return x

# 测试

print('mul(5) =', mul(5))

print('mul(5, 6) =', mul(5, 6))

print('mul(5, 6, 7) =', mul(5, 6, 7))

print('mul(5, 6, 7, 9) =', mul(5, 6, 7, 9))

if mul(5) != 5:

    print('测试失败!')

elif mul(5, 6) != 30:

    print('测试失败!')

elif mul(5, 6, 7) != 210:

    print('测试失败!')

elif mul(5, 6, 7, 9) != 1890:

    print('测试失败!')

else:

    try:

        mul()

        print('测试失败!')

    except TypeError:

        print('测试成功!')


  • 1

Reply