Python Loops

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/lessons/python-functions-loops-cc/exercises/exponents

Why do I get (many times in different exercises) a ‘wrong answer’ while actually my output is correct.
If I put in this answer:

#Write your function here

result = []

def exponents(bases, powers):

  for b in bases:

    for p in powers:

      result.append(b ** p) 

  return result

#Uncomment the line below when your function is done

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

The output of running the code is:

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

Exactly as requested. However, the program returns:

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]

This happens quite often and is quite frustrating. The only way to continue to the next exercise is to view solution, which then often shows the exact same answer. (I didn’t yet check this one as my output is correct so this is at least one road that leads to Rome :slight_smile:

You miss certain corner cases. For example, if we called the function multiple times:

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

you will see that the second function call contains way too much data.

Often when there is an error, there is a good reason for it. Don’t assume the error is unjustified, instead, take a step back and ask yourself what could cause the error

Calling a function multiple times should go well. Functions are designed to be used multiple times

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.