Discuss / Python / 交作业

交作业

Topic source

def product(*x):

    sum = 1

    if x is ():

        x = 0

    for n in x:

        sum = sum * n

    return sum

写作业时出了几个错,希望自己注意:
1.定义函数时:添加条件for in x写成了for in product,导致出错
2.定义函数时,未审题下方的测试,x前方未加*,导致出错
2.在添加条件if x is None时,无论如何都是测试失败,还试着加了isinstance,结果更是一团糟,看了下方评论才知道()空值不代表none。所以出错

 if x is ():

        x = 0

这一行我可以理解为为可变参数x设置默认值吗?

把x变成整数型,然后for循环无法迭代会报错,抛出异常。


  • 1

Reply