Discuss / Python / 求大佬解答,为什么最后一个是“测试失败”?

求大佬解答,为什么最后一个是“测试失败”?

Topic source

def mul(*nums):

    consequnce = 1

    for x in nums:

        consequnce = x * consequnce

    return consequnce

结果:

mul(5) = 5

mul(5, 6) = 30

mul(5, 6, 7) = 210

mul(5, 6, 7, 9) = 1890

测试失败!

else:
    try:
        mul()
        print('测试失败!')

传入参数不能为空,至少要有一个参数。*nums初始为空

Jerry bonus

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

在检查一下参数是否为空就可以了,

raise TypeError('lack of parameters')


  • 1

Reply