Discuss / Python / 作业

作业

Topic source

chengww5217

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
def product(x, *args):
    for i in args:
        if not isinstance(i, (int, float)):
            raise TypeError("bad operand type")
        x *= i
    return x
# 测试
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('测试成功!')

姬_宫_涅

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

大佬带带我

艾文SEO

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

不错啊

为什么这个代码复制到sublime中保存之后,再到cdm命令行中运行这个文件无效。

大佬,请问,最后两行是什么意思?前面都看懂了,就是最后两行没有看懂

    x *= i
return x

永远短毛

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

你自己代码里不是也有吗,你不懂那你咋写的。。 x *= i 就相当于 x = x*i


  • 1

Reply