Help with Python 3 course 'Exponents'

I am currently doing the Python 3 course to learn it from complete scratch and I am having trouble with a specific Task. I have written following code and while I am aware its probably not even close to the most effective way I could have written it, I still think it should work.

This is the link to the lesson and below is the code. Everytime I use it it gives out

[2, 4, 8, 3, 9, 27]

instead of

[2, 4, 8, 3, 9, 27, 4, 16, 64]

def exponents(bases, powers):
  math = []
  length = len(bases)
  while length > 0:
    for i in bases:
      math.append(bases[0] ** powers[0])
      math.append(bases[0] ** powers[1])
      math.append(bases[0] ** powers[2])
    return math
  length = lenght - 1

Help of any kind would be highly appreciated since I don’t understand why it only prints the exponents for the first 2 numbers in bases.

there are a couple of things important:

using the return keyword means handing back data to the caller, which means the function is completed. Just like when making a test in school, you can decide to hand back the test to the teacher without filling in all the questions. Same with your loops, not all iteration are made because you decide to hand back/return before all the loop iterations are done

i wouldn’t remove elements from the list(s), its not needed, only complicate things

furthermore, you can’t assume the powers list contains 3 elements, it might contain 1, 2 elements or a lot more.

you really need two loops, one to loop over bases, then within this loop over powers and append to math list

both loops can be for loops, given in both cases the amount of iterations is known (amount of items in the list), a while loop is useful when you don’t know how many iterations you need to make


@code7484294848, this is highly instructive! Please run this code:

bases = [0,1,2,3,4,5,6]

for i in bases:

What output do you expect?

What is the actual output?

What is going on? Hint: use print() to keep track of i.


Output I expect:
That it prints each number in bases once

Actual Output:
It only prints first 4 numbers then stops

What is going on:
I’ve tried to use print(i) instead of what I did before and it printed all values, still don’t understand though why it doesn’t print all numbers the other way but exactly 4 numbers

removing elements from the list you are looping over is almost always a bad idea, like i said before:

what @patrickd314 means was of course to combine print with remove:

 bases = [0,1,2,3,4,5,6]

for i in bases:

then you will see that elements are skipped. why is this?

lets say you remove the first element (index 0) from the list, this spot can’t stay empty. So everything to the right of the removed item shifts one position to the left:

bases = [0,1,2,3,4,5,6]



see? combined with a loop this causes the skips, given the loop goes to next index after all the elements have shifted.

but we are completely focusing on the wrong thing, i am going to repeat myself, again:

we don’t need pop or remove, it only complicates the problem we are trying to solve.