Python Loops Challenge: I need clarification, thank you

Hi everyone,
Please I need your input. My code is not running as expected. Your input will be appreciated. Thank you

My code below:

  1. Create a function named exponents() that takes two lists as parameters named bases and powers. Return a new list containing every number in bases raised to every number in powers. For example, consider the following code. exponents([2, 3, 4], [1, 2, 3]) the result would be the list [2, 4, 8, 3, 9, 27, 4, 16, 64]. It would first add two to the first. Then two to the second. Then two to the third, and so on.

My code below:

def exponents(bases, powers):

for i in bases:

for j in powers:

  a = bases[i]**powers[j]

  return a

#Uncomment the line below when your function is done

print(exponents([2, 3, 4], [1, 2, 3]))

1 Like

When you set a to equal value in this for loop you showed, it’s resetting the value in every loop iteration.

So think about the way to avoid having it reset.
Also consider what the values i and j are in this loop (make print statements for each one to verify they are what you think they are).

Sometimes it helps to draw out on paper what your code is doing so you can isolate the potential pitfalls.

Does this return 2 and stop? What did you assign to the value a before returning it?

def exponents(bases, powers):
  for i in bases:
    for j in powers:
      a = bases[i]**powers[j]
      return a

Is that your code?

1 Like

Yea, this is my code

Take a look at the advice @toastedpitabread offered. Draw it out on paper to see if you get more than one iteration. That’s what my questions are asking about.

1 Like

okay, thank you. I will do that

yea, i’m not trying to be obtuse.

A big part the goal of these problems is to get better at problem-solving, not just get the answer. Hence, my wording was just to give you some tips as to how you might approach it. This will come up a lot, and part of being a coder and enjoying it is having tools with which to break down problems.

1 Like

Take a look at where your return statement belongs. Do you want to return during the loop or after the loops are completed?