def exponents(num1,num2):
    if (num2==0):
        return 1
    else:
        return num1*exponents(num1,num2-1)

Hey I was wondering how I can generate a list of exponents using a for loop or a while loop so that the numbers display like this:

Input (2,3)
Output 2,4,8

What I have so far only returns the answer,

Please assist if possible, I am really not getting the hang of this loop thing.

To do it recursively as you code, I would use recursive generator with for:

>>> def exponents(num1,num2):
    if (num2==0):
        yield 1
    else:
        for r  in exponents(num1,num2-1):
            yield r
        yield num1 * r


>>> list(exponents(2,3))
[1, 2, 4, 8]

In iterative version you could yield each subresult, or append to list if you need to return list.

Thanks for the help. I did not want to do the problem with the recursive but that was the only way i could do it, is there a way to do it with the ** cause I think I am doing it the hard way. I just did it that way cause i remember it from a while ago

Something like this perhaps? but this code does not work

def exponents(num1,num2):
    if (num2==0):
        return (1)
    else:
        for r in exponents (num1**num2):
            return exponents

Ok you really are getting it hard. You are not needing to recurse, here is list comprehension version, you can alter it to append and for loop, if list comprehension is not yet covered for your course (this includes 1)

def exponents(base, power):
    return [base ** power for power in range(power + 1)]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.