思路: 因为传入 product 函数的内容是不确定的,所以使用可变函数 *numbers 来接收内容,并且在运算前进行判断是否输入的数据长度为空,为空就返回TypeError错误。
product
*numbers
TypeError
Code:
def product(*numbers): if len(numbers)==0: # 判断输入的数据长度是否为空 raise TypeError # 返回 TypeError 错误。 sum = 1 for n in numbers: # 循环所有传入值并且相乘 sum = sum * n return sum
谢谢!
Sign in to make a reply
护苗2018
思路: 因为传入
product
函数的内容是不确定的,所以使用可变函数*numbers
来接收内容,并且在运算前进行判断是否输入的数据长度为空,为空就返回TypeError
错误。Code: