以下函数允许计算两个数的乘积,请稍加改造,变成可接收一个或多个数并计算乘积 def product(x, y): return x * y
# -*- coding: utf-8 -*- def prodouct(*num): n = 1 for x in num: n = n * x return n # 测试 print(‘print(5)=‘,prodouct(5)) print(‘print(5,6)=‘,prodouct(5,6)) print(‘print(5,6,7)=‘,prodouct(5,6,7)) print(‘print(5,6,7,9)=‘,prodouct(5,6,7,9)) if prodouct(5) != 5: print(‘测试失败‘) elif prodouct(5,6) != 30: print(‘测试失败‘) elif prodouct(5,6,7) != 210: print(‘测试失败‘) elif prodouct(5,6,7,9) != 1890: print(‘测试失败‘)
原文地址:https://www.cnblogs.com/chling/p/11737070.html
时间: 2024-11-10 07:44:47