[英]How can I use recursion to find increments of a number in python?
I have the following code, but it just doesn't work when I run it. 我有以下代码,但是在运行时它不起作用。 I would like it to return something like: [5,10,15,20] if the inputed value for n is 4. Any advice is very appreciated.
我希望它返回如下内容:[5,10,15,20]如果n的输入值为4。非常感谢任何建议。
def MultipleRecursive(n):
multiples=[]
if n==0:
multiples.append(n)
else:
Total=5*MultipleRecursive(n-1)
multiples.append(Total)
return multiples
A trivial version is: 一个简单的版本是:
def mr(n):
if n == 0:
return []
return mr(n-1) + [5*n]
You can try this: 您可以尝试以下方法:
def rek(n):
list = []
if n == 0:
return list
else:
list = rek(n-1)
list.append(5*n)
return list
print rek(4)
>>> [5, 10, 15, 20]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.