Reverse


#1



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


I am printing the letters in reverse, and they have all been appended to an empty list, so why isn't it accepting my code?


def reverse(text):
    reversed_text = []
    count = 1
    for i in text:
        
        i = text[len(text) - count]
        reversed_text.append(i)
        count += 1
    print(reversed_text)

reverse('chane')


#2

You are somewhat overthinking this... Try following this template:

def FUNCTION(PARAMETER):
    VARIABLE_1 = 0
    VARIABLE_2 = ""
    for INDIVIDUAL_ITEM in PARAMETER:
        VARIABLE_1 += 1
        VARIABLE_2 += PARAMETER[len(PARAMETER)-VARIABLE_1]
    return VARIABLE_2

If you get it to work, don't just move on. Make sure you understand why and how it works. If you have any questions about why and how it works please just ask.


#3

i disagree with @aquaphoenix17 given you are so close to a working solution.

you have the reversed_text, but its a list. You can use .join() to convert list to string, then return the reversed string


#4

That works too...

I was just trying to show a more efficient way of solving the problem. Then again, perhaps efficiency isn't your top priority until you actually start asking about it...


#5

it isn't much more efficient then thedogis2 solution, in fact, when looking at preformance, i would go with thedogis2 solution given strings are immutable in python, and lists are mutable


#6

:sweat:

Welp, ... to each their own I guess.


#8

2 posts were split to a new topic: Reverse


#9

2 posts were split to a new topic: Reverse


#10

Strings are immutable, but replacement is still a valid approach.

s += char

This does not append s, but replaces s with s + char, a new string.


#17