简体   繁体   中英

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM