Unique REVERSE solution trying to make it work


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#


Ok, so I've been trying to use the pop() method to solve this problem. And the end result does work on the console but for some reason I can't pass the section because apparently it just returns "None".


I can't figure out why this solution wouldn't work. Yes, it's more convoluted than using the range step method but I think this should work.


def reverse(text):
    product= ""
    producter=""
    producter=text
    text = list(text)
    for i in range(len(text)):
        for letter in text.pop():
            product +=letter
        if len(product) == len(producter):
            print str(product)


#2

Only these lines are needed...

reverse_text = ""
text_iterable = text
text = list(text)

Now we are free to destroy text. Iterate over the copy, and pop from the original. This ensures a full count of reversed characters making the last conditional also unnecessary.

Eg.

>>> text = "Python!"
>>> reverse_text =""
>>> iter_text = text
>>> text = list(text)
>>> for i in iter_text:
	reverse_text += text.pop()

	
>>> reverse_text
'!nohtyP'
>>>

We don't make any special use of i other than to use it as a dummy variable that serves the iteration process.


#3

That is just what your function does, I'm afraid. In line 10 you print out the finished string, but you should really return it to whatever called the function.

Hope that helps

Tim F


#4

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