Discuss / Python / 试试 看

试试 看

Topic source

def product(x,*args):

    s=x

    for z in args:

        s=s*z

    return s

# 测试

print('product(5) =', product(5))

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

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

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

if product(5) != 5:

    print('测试失败!')

elif product(5, 6) != 30:

    print('测试失败!')

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

    print('测试失败!')

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

    print('测试失败!')

else:

    try:

        product()

        print('测试失败!')

    except TypeError:

        print('测试成功!')

V客Zylan

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

 try:

        product()

        print('测试失败!')  #什么情况下才会执行这一段呢????

    except TypeError:

        print('测试成功!')

张康除

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

如果product函数,接收0个参数,没有报错(也就是正常运行)的话,就会print('测试失败'),因为题目要求计算一个和一个以上数的乘积。


  • 1

Reply