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?

Hi everybody,

I tried that but I have a wrong answer:
< exponents([2, 3, 4], [1, 2, 3]) should have returned [2, 4, 8, 3, 9, 27, 4, 16, 64] , and it returned [2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64]/>

with these lines of code:

<>
new_lst =
def exponents(bases, powers):
for num in bases:
for exp in powers:
new_lst.append(num ** exp)
return new_lst
</>

return new_lst is same indentation than for num in bases

How do you use </> you put “p” inside or something else?

Thx

you are missing a “/” on the top one.

for num in bases it will first go through 2, then 3 then 4
for exp in powers it will then go to 1, 2, 3 for each of the above.

for num in bases it will first see 2.
for exp in powers it will loop through each one of the values in the list [1, 2, 3].
2 ** 1
2** 2
2** 3

After that it goes back up to for num in bases and then go to 3 then goes down to for exp in powers and does the same thing it will loop through the list [1, 2, 3]

3 ** 1
3 ** 2
3 ** 3

This is why you are getting each number in bases to each number in exp, in the way you are writing the code.

If you want the 0 index in bases to ** with 0 index of powers, then I would use indexes. I’m sure this is not the only way to do it, but that’s my suggestion.

Be sure to define the new list inside the function else it doesn’t get reset with each call made, but keeps growing.

def func(foo, bar):
    result = []

is there a way to do this with list comprehension? Thanks

Yes, comprehensions permit nested loops.

>>> def exponents(bases, powers):
    return [b ** x for b in bases for x in powers]

>>> print(exponents([2, 3, 4], [1, 2, 3]))
[2, 4, 8, 3, 9, 27, 4, 16, 64]
>>> 

Ahhhh thank you. I had “new_lst = [base ** power for base in bases AND power in powers]”

“and” instead of “for”

Also, didn’t even occur to me that I didn’t need a variable. Thanks for your help

1 Like

You’re welcome. Now add to the challenge so that the result looks like,

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

Later…

Have you given this any consideration? It might help to know that as we can write a comprehension on two lists (or dictionaries) and use nested loops to yield two values, we can also write a comprension on a comprehension that still yields two variables, but also lets us group the results according to one of the variables.

Hi. Could someone please explain why this code results in a SyntaxError?

def exponents(bases, powers):
	new_lst = []
	for i in range(len(powers)):
		i = range(len(bases[0:]) ** powers[i]
		new_lst.append(i)
	return new_lst

When you see a syntax error in Python thrown on a line that you know to be correct, look to the preceding line for a missing close-quote or close-parentheses.

Whoops. Thanks for the help. Fixed the mistake, but now I’m getting a “TypeError: unsupported operand type(s) for ** or pow(): ‘range’ and ‘int’”.

def exponents(bases, powers):
	new_lst = []
	for i in range(len(powers)):
		i = range(len(bases[0:])) ** powers[i]
		new_lst.append(i)
	return new_lst

What you want to happen in the first line of the ‘for’ block isn’t going to work. range() does not work that way. I think you mean for every integer in the range() to be raised to a power. Although you could find some functionality close to that using NumPy, it won’t work in native Python. You will need to retrieve each item in range() (just as you do in the ‘for’ loop) to work with individually.

Hint 1: You’ll need a nested ‘for’ loop.

Hint 2: Because of Python’s magic ‘in’ operator, you don’t need any indexes (or range()) at all!

lst = ['a', 'b', 'c']

for idx in range(len(lst)):
    print(lst[idx], end = " ")
    
print()

for item in lst:        # no indexes!
    print(item, end = " ")

# Output:
a b c 
a b c
1 Like

Ah, got ya. My original solution was:

def exponents(bases, powers):
	new_lst = []
	for b in range(len(bases)):
		for p in range(len(powers)):
			result = bases[b] ** powers[p]
			new_lst.append(result)
	return new_lst

However, I can see that some of the code is redundant:

def exponents(bases, powers):
	new_lst = []
	for b in bases:
		for p in powers:
			result = b ** p
			new_lst.append(result)
	return new_lst

Cheers!

Or given to using the index for read-only purposes. We only need to use this functionality if the intent is to be able to modify the value of list items. No harm in writing code that way, as long as we see what it is doing… Giving us write permission.

The technical difference is that one, in range() uses the members in a range sequence as indices for accessing values; whereas, the other in sequence uses the members of the sequence as read-only values taken directly from the list.

There is also a difference in which methods are used in the background but I’m not the best at describing that much detail so will defer to better information sources than the generalized picture we can give here.

From a practical view, if all we wish our function to do is read the sequences we give them, the in sequence is the way to code that particular function.

Whatever your decision, do not code one way or another because someone told you it is better. Write however you wish and discover for one’s self what might be more suitable to a given situation. In other words, and I hate to say this, don’t listen to the don’ters unless you clearly understand and agree with what they’re saying not to do. Otherwise ignore and keep on coding as you would given whatever the prevailing circumstances.

Right. The truth is that I did not actually know why the suggestion made by @patrickd314 was a superior one, other than his code being shorter and therefore supposedly more efficient.

It is becoming clear to me that one can do something well without actually understanding what it is that he is doing, at a core level. I have heard a similar sentiment towards those who graduate a 4 month intensive coding boot-camp, when compared to CS graduates who have gotten to grips with the intensive maths that is fundamental to software engineering.

1 Like

Hey, could someone help me understand why im getting an error with my code on this question??

Heres my result

result = []
def exponents(bases, powers):
   for b in bases:
      for p in powers:
         result.append(b ** p)
   return result

when i look at the right side of the screen, the result that is printed is exactly correct! yet the module is telling me im wrong…
exponents([2, 3, 4], [1, 2, 3]) should have returned [2, 4, 8, 3, 9, 27, 4, 16, 64] , and it returned [2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64]

what i dont understand is why the correct answer is printed, i had the exact same issue on another question…
thanks!

>>> result = []
>>> def exponents(bases, powers):
	for a in bases:
		for x in powers:
			result.append(a ** x)
	return result

>>> exponents([2, 3, 4], [1, 2, 3])
[2, 4, 8, 3, 9, 27, 4, 16, 64]
>>> exponents([2, 3, 4], [1, 2, 3])
[2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64]
>>> exponents([2, 3, 4], [1, 2, 3])
[2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64, 2, 4, 8, 3, 9, 27, 4, 16, 64]
>>> 

The result list needs to be initialized inside the function, else it never gets reset.

1 Like

Hi, when I practiced this task, this was what I wrote:

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

it results in three lists:
[[2, 4, 8], [3, 9, 27], [4, 16, 64]]

However, the answer is
[2, 4, 8, 3, 9, 27, 4, 16, 64]
which is just one list, combining together
the answer code is :

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

I still can’t figure out why make mine and the result of the standard answer code different.
If anyone can tell me why I would be grateful!