FAQ: Code Challenge: Loops - Exponents


#1

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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!


#2

This is the function I used here, it returned ‘None’. But why is it wrong? Why do I need a second line to return new_lst, why can’t I combine it in one step?
I feel like I’m missing(or forgot) some basic knowledge which would make this clear to me.

def exponents(bases, powers):
new_lst =
for base in bases:
for power in powers:
return new_lst.append(base ** power)

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


#3

Next time try using the </> symbol for code so that it keeps the indentation.

I think this is what you have?

def exponents(bases, powers):
  new_lst = []
  for base in bases:
    for power in powers:
      return new_lst.append(base ** power)

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

.append() is a method which performs an action toward new_lst and doesn’t return anything.

I think you want to .append() the list after the powers in powers loop (where it is at now) but not return it. At the end after the list is created (after both for loops) then return the new_lst .

Here is an example of what you tried to do:

x = []
x.append(1)
y = x.append(2)

print(x)
print(y)

Returns

>>> [1, 2]
>>> None

See how y returns None? That is because .append() doesn’t return a value, but it still performed the action to append x. That’s kind of what you tried to do in your code.


#4

Thank you for the tip, I didn’t realise my code would be copied as flat text, but you did get the code right!

And thanks for the example, I do understand that one. In your example y gets ‘None’ because the 2 get appended to x, leaving y without any value, right?
So for mine, I first have to state new_lst.append() before I can return it. If I don’t state that first, it doesn’t have anything to return yet, since it was getting appended after I typed return. Is that making sense?


#5

No, not really. .append() performs an action on an already existing list. It modifies the list in place instead of of returning a new list. You’re .append() is acting on the list you specify to make the addition to it.

Here’s a hopefully not confusing analogy. It’s like having a basket with one red apple. When we say basket.append(green apple) it’s an action that places a green apple in the basket. It doesn’t create a new basket, it doesn’t report what’s now in the basket. It’s purely the action. It’s putting that green apple in the basket.

So when you ask the action what it physically is or ask the action to report itself. It isn’t physically anything (isn’t data) . Thus it is None. It’s not a physical thing that reports or returns anything. It’s simply the action only. You can call the basket and see what is in after the action ( one red apple and one green apple) it because it is physical thing or actual data in the case above with the lesson.

That’s how I understand it.


#6

Ooooh! Now I see, your analogy was really clear!!

I can’t return the basket.append(green apple). But after the green apple is put in there, I can return the basket to see what is in there now. I was trying to do it during the action, but I needed to look inside the basket afterwards, and not at the action itself.


#7

Something else of note that is not immediately obvious, list.append() has no return, so when we,

return my_list.append()

all that is seen by the caller is nothing.

>>> def add_to_list(my_list=[]):
	return my_list.append('a')

>>> add_to_list()
>>>