FAQ: Code Challenge: Loops - Exponents

Sorry, got called away.

Above we talked about using x and y. That’s what base and power are. Only thing is we’re passing each pair to a function that raises one to the power of the other. That’s what functions do. Take parameters and do something with them. In this case it’s computing the value a x raised to the power of y.

Don’t mind my frankness, and do keep asking questions as long as this is giving you them.

1 Like

Totally, i completely appreciate it, i’m really learning a lot with these answers, by all means, be frank

1 Like
new_list = []
def exponents(bases,powers):
  for i in bases:
    for p in powers:
      new_list.append(i**p)
  return new_list

It’s saying that my output is:

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]

But I tested this code outside of Codecademy and it outputted the correct answer?

That declaration should be made inside the function so it gets reset on each call.

1 Like

Ah right I see, much appreciated.

1 Like

I got the right answer but am wondering if there is a downside to this way of approaching it. I don’t want to solve this (or other problems) in manners which won’t work when things advance. Thanks!

def exponents(bases, powers):

  final = []

  for x in bases:
    for y in powers:
      final.append(x**y) 
  return final

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

Quit thinking that way. There is no downside to code that works. Period. Will you come back to this and find another way to write it? That’s up to you, but if it was me then, yes.

Would it improve the code? Hardly something we could determine. Would it add to my tuition? Yes. It would be another kick at the can, as it were. The last thing we as programmers should be looking for is a subjective better. It blocks our intuition from seeing what actually might improve our program. In my code I look for weakness and then work to strengthen it.

def exponents(bases,powers):

new=

for i in bases:

for x in powers:

  y=bases[i]**powers[x]

  new.append(y)

return new
for this code i got- IndexError: list index out of range

def exponents(bases,powers):

new=

for i in range(len(bases)):

for x in range(len(powers)):

  y=bases[i]**powers[x]

  new.append(y)

return new
but this code worked .
so what is the difference when we use range and when we dont ?

The greatest difference is in what the iteration variable holds.

  • for i in range() => i is an index
  • for x in iterable => x is the element value

Why do this code return duplicate numbers of the list again in python .

newlist=

def exponents(bases,powers):

for item in bases:

for i in powers:

  powe=item**i

  newlist.append(powe)

return newlist

Error shown as below
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]code

Hello @zohrarafei and welcome to the Codecademy Forums!

Your code seems to work; I’ve run it myself and it outputs the correct list.

Hello, @zohrarafei, and welcome to the Codecademy Forums!

Your exponents function should be written so that it produces correct results even if you call it more than once in the same program. However, because you initialized newlist outside the function, that list gets initialized to [] only once, regardless of how many times you call the function. Consequently, after the first time exponents is called, newlist always contains data remaining from previous calls. Accordingly, results from every call except the first one are incorrect.

Move this statement to inside the function, right after its header:

newlist=[]
1 Like

I understand why the codeacademy answer works for this one but I can’t figure out whats the problem with my code below, if anyone could help explain the problem I’d really appreciate it.

def exponents(bases, powers):

i = 0

new_lst =

while i < len(bases):

new_lst.append(bases[i]**powers[0:])

i += 1

return new_lst

We need to iterate separately both lists, the bases, and the exponents which means a nested list so that all possible permutations are found.

Thanks for responding, so I kind of get what you’re saying but what’s wrong with my code as I read it executes by appending the first base to the power of all the exponents and than it carries that on until it finishes so I’m still not sure why I would get a different result. I get this error ‘TypeError: unsupported operand type(s) for ** or pow(): ‘int’ and ‘list’’ I can hardcode it like this :def exponents(bases, powers):
new_lst =
i = 0
while i < len(bases):
new_lst.append(bases[i]**powers[i])
new_lst.append(bases[i]**powers[1])
new_lst.append(bases[i]**powers[2])
i += 1
return new_lst // But this obviously isn’t the optimal solution.

That is a list object so has no ** or pow() method.

A for loop is probably better suited to this problem.

results = []
for base in bases:
    for power in powers:
        results.append(base ** power)

The inner loop does what your three lines above do. Consider what happens if there are more than three powers? A function should be able to adapt to variable inputs.

1 Like

Thanks, I get it now

2 Likes

Hello,

Why does this return none? I understand that I don’t need to define another variable as = new_list.append() but I’m wondering why if I do that, it returns none logically.

#Write your function here
def exponents(bases, powers):
new_lst =
for base in bases:
for power in powers:
new = new_lst.append(base ** power)
return new

#Uncomment the line below when your function is done
print(exponents([2, 3, 4], [1, 2, 3]))

1 Like

list.append() cannot be assigned since it has no return value. Appending is an in-place operation with no other side effect.

Return the list that is being appended.

1 Like