所以作为python编程类的作业,我们应该相互乘以整数(n,m)而不使用*符号(或其他乘法形式)。我们应该使用递归来解决这个问题,所以我尝试了将n与自己相加,m次。我认为我的问题是使用递归本身。我在网上搜索了递归使用,没有结果。这是我的代码。有人能指出我正确的方向吗?如何使用递归不带*符号的乘法?
def mult(n,m):
""" mult outputs the product of two integers n and m
input: any numbers
"""
if m > 0:
return n + n
return m - 1
else:
return 1
提示:通常情况下,递归涉及的函数的代码路径中的至少一个呼叫_itself_。 – mgilson
尝试根据基本情况和递归情况来定义乘法。基本情况是* zero *,而不是1,因为0的任何东西总是0.递归的情况是'n + mult(n,m-1)',所以'n'被加到'n'和' m'减去1. –
看看[在Python中构建递归函数](http://stackoverflow.com/questions/479343/how-can-i-build-a-recursive-function-in-python)或它的链接问题[Python的递归基础知识](http://stackoverflow.com/questions/30214531/basics-of-recursion-in-python) – JGreenwell