My solution works but the editor is throwing a code


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096
When I run this code it prints out "!nohtyP" just like it's supposed to, however I'm getting this error below.

Oops, try again. Your function fails on reverse("Python!"). It returns "!nohtyPcbaPython!" when it should return "!nohtyP".

Is there something i'm missing? It's returning the correct value since it's printing the exact string it's supposed to.


letlist = []
def reverse(text):
    for i in text:
        letlist.append(i)
        # print letlist
    for i in text:
        count = 0
        letlist.insert(count, (letlist.pop(letlist.index(i) - count)))
        # print letlist
        count = count - 1
    return ''.join(letlist)
print reverse("Python!")


#2

if you add a second function call, you will see letlist is accumulating data because its not declared inside the function, so the list isn't empty when we attempt to reverse a second or third string.


#3

Thank you, Stetim. I was encountering a similar issue with the "Purify" practice problem in which this was my code:

newnums = []

def purify(lst):
    #newnums = [] goes here
    for num in lst:
        if num % 2 == 0:
            newnums.append(num)
    return newnums

I did not realize that Codecademy didn't re-initialize the code with each test and thus does not reset the empty list.


#4

this has nothing to do with codecademy? calling the function multiply time simply causes a problem:

newnums = []

def purify(lst):
    #newnums = [] goes here
    for num in lst:
        if num % 2 == 0:
            newnums.append(num)
    return newnums

print purify([1, 2, 3, 4, 5, 6, 7 , 8, 9])
print purify([4, 5, 5, 4])

see how the second function call still contains data from the first function call? This shouldn't be

furthermore, the great thing about function is that we can put them in separate file and import then (good practice for when you have a 1000+ lines of code for example), but if we try to import your function:

from your_script import purify

we would get a newnums referenced before assignment error

which is why codecademy rejects the code. Which i think is fair :wink:

I hope my explanation helps you understand why defining newnums inside the function is important


#5

Thank you! This definitely helped, and also helps explain why I was having issues. I ran the code through a separate website that walks through code line by line and it worked just fine. It's not that Codecademy did something wrong, just that I made a silly error that allows the code to run fine on its own, but not when imported, like I'm sure is done when Codecademy checks the solution. :sweat_smile:

Anyways, thanks again for the explanation!


#6

I found the issue. When I ran my code with the input "Hello world!" it returned a bunch of gibberish and not "!dlrow olleH". I realized letlist.index(i) could technically be in multiple indexes since it was checking the index of a specific character which could be in multiple locations.

After a bunch of modifications, I got my code to work. The reason it was printing "Python!" in reverse properly was that "Python!" doesn't have duplicate characters. Either way thank's for all of your help!


#7

yes, index() will return the index of the first match found, so having duplicate characters is a problem indeed.

Good that you figured it out :slight_smile:


#8

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