Discuss / Python / 练习

练习

Topic source

Neri.

#1 Created at ... [Delete] [Delete and Lock User]
def mul(*x):
    s=1
    len_s=len(x)
    if(len_s==0):
        raise TypeError('输入内容不能为空!')
    else:
        for i in x:
            s=s*i
    return s



# 测试
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('测试成功!')

有梦不难

#2 Created at ... [Delete] [Delete and Lock User]
# -*- coding:utf-8 -*-def mul(*number):    if number==():        raise TypeError('输入内容不能为空!')    else:        n=len(number)        sum=1        for a in number:            sum=sum*a    return sum

  • 1

Reply