【问题标题】:How to get the value of recursive product using negative input?如何使用负输入获得递归乘积的值?
【发布时间】:2015-03-16 10:37:45
【问题描述】:

因为我发现很难得到

看看:

def mult(a, b):
    if b == 0:
        return 0
    rest = mult(a, b - 1)
    value = a + rest
    return value

prod = int(input('Enter number: '))

print('The product of', prod, 'x', prod, 'is', mult(prod,prod))

【问题讨论】:

  • 你能修正你的格式吗?我怀疑你的 mult 函数没有正确缩进。
  • @Andy 好的,我会解决的

标签: python python-3.x python-2.7 jupyter-notebook spyder


【解决方案1】:

刚开始

def mult(a, b):
    if b<0: return -mult(a, -b)

并继续使用您现在拥有的代码(假设后者的缩进正确 - 现在它有点乱,但我认为这只是一个格式问题,如果正确编辑代码将起作用:-)。

【讨论】:

  • @RomeliiGaballoAlbia,总是乐于提供帮助——记得在允许接受的几分钟内接受这个答案(点击答案左侧的大纲复选标记) !-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
相关资源
最近更新 更多