FAQ: Code Challenge: Loops - Exponents

This community-built FAQ covers the “Exponents” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Exponents

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

7 posts were split to a new topic: Why does list.append() return none?

4 posts were split to a new topic: Why does my list get longer everytime its run?

6 posts were split to a new topic: How can the exponents challenge be done with list comprehension?

7 posts were split to a new topic: Should I be using range in my for loops?

2 posts were merged into an existing topic: Why does my list get longer everytime its run?

3 posts were merged into an existing topic: How can list comprehension loops be nested?

3 posts were split to a new topic: How can list comprehension loops be nested?

My code works perfectly except that I keep getting a “None” at the end of the result. Could someone tell me what’s wrong with it?

def exponents(bases, powers):
  for base in bases: 
    for power in powers:
      print(base ** power)

Also, I found on the internet that “print” should have been “return” if I don’t want a “None”, but when I use “return” the result would only be “2”.

return terminates a function and resumes program flow from the caller. If it is inside the loop, the function never gets a full run.

1 Like

There is one result. Not many. Not zero.
If the result is many things then you can use a list or some other container.

1 Like

Got it, thanks a lot!

What is wrong with my code:

def exponents(bases, powers):
empty =
for i in bases:
for y in powers:
return empty

I saw the answer and where I was wrong was:


Can someone please help explain what is wrong with my original code?

As I’m sure you’ve found out by now, it looks like you missed the line of code which assures that every item/base in bases gets raised by every power/item in powers.
It may look something like this:

“for n in range(1, len(bases)**2):”

It’s your most outer layer of your nested loops, that tells your function how many list items should be present in your new list.

Also i would set a variable to append instead of the expression (in your 5th line)
If may look something like this:
ex = i**y
return empty

…instead of:
return empty


I’m really having a hard time with lists. I dont understand how you can access 2 list and pull one element from one and then make it interact with another one. I’m trying to figure it out by myself, but I could use some help.

Here is my code

Here is where pencil and paper are invaluable. It allows us to sketch and visualize.

Write two lists (five items is enough in each) side by side on a piece of paper. Do this before reading the next instruction.

Ask yourself, do these lists have any relation to each other? The answer to that would suggest pairing, if yes. Otherwise a relation can be defined simply by creating connections.

The exercise relates two lists that are not related in any way. Still, we create a relation by pairing every value in one list with every value in the other list then pass those pairs to the same function and accumulate those results.

That’s where the nested loops are employed. In order to create the complete set of paired values, we need to iterate through one list, and pair each value with every value in the other list. That’s why we iterate the second list each time.

In order to be able to test it would help to have a link to this exercise. Please post it in your reply. Thanks.

D’oh! Link at top of thread. Please belay.

There is more to come, and some of it will seem easier, but, work with this first until you have firm grasp. Nobody is pushing you to the next exercise. We want you to have a full and complete understanding before going ahead. It’s for your benefit, not ours.

1 Like

Thank you so much for your support

Yes, I’m trying to really grasp the concepts and not take the easy way out. So far I understand, a nested for loop is the way to go. The first for loop will go through the lists, the second one to the individual elements in those lists.

What I can’t grasp Is how to relate the len of the second list to just an individual element of the first list, and the move on to the second element and so on.

here is the code so far

Correction, the first loop iterates through one list, the second loop iterates through the other.

That is where the nesting comes in.

for x in i:
    y in j:
        # do something with x and y - this is your pair

i is a list, as is j. x and y are individual values from each respective list.

1 Like

Ok now this is interesting. I tried changing the code, i think i almost got it, but I’m still not quite getting there, take a look. What an I missing here?

ok so i ran the solution, but i still dont get it.

I thought the exercise was supossed to count the elements in powers, add them up and them use the use the base. Thats why i wrote this code

Instead the explanation was easier, but it got me more confused

Can i please get some clarification?