7.reverse help me!


#1

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

I don't understand the error message, and I don't understand why my code isn't working.
I have included comments in the code to show what i'm expecting it to do

I get the error message:
"Oops, try again. Your code looks a bit off--it threw a "'str' object has no attribute 'insert'" error. Check the Hint if you need help!"


def reverse(text):
    for char in text: #iterate through each character in text
        reverse_text = ""
        start = 0
        x = len(text)-1 #starts from the final index of text
        end = text[x] #takes final character of text variable
        reverse_text.insert(start, end) #inserts last character of text at begining of reverse_text
        x -= 1 #moves along the index of text from right to left
        start += 1 #moves to the next index of reverse_text starting at the left
    return reverse_text


#2

You're trying to use .insert() on reverse_text which is an empty string and not a list.


#3

Can you not insert stuff into strings? Or will it work if I change it to a list?

Many thanks,
Dominic Airoll


#4

You cannot use .insert() on a string, if you want to add stuff in string maybe try using + or +=

"Hello " + "world!" will be "Hello world!"


#5

Using Insert complicates things. You'd be working from both sides towards the middle. Your reverse_text is a string not a list, it would have to be reverse_text = [] but it would still throw back some errors. Insert just isn't a good tool for this problem.

My code was similar to this, minus a few steps. Here it is for comparison.

def reverse(text):   ## ***note: counts of strings start at 0***
    l = len(text)-1  ## This will be the last character of the text 
    rword = []       ## This is where each character will be stored
    while l >= 0:    ## While the character place is equal or greater than 0
        rlet = text[l]  ## rlet will be the character in the pos of l
        rword.append(rlet) ## This adds the chacter to a new list
        l -= 1             ## Moving the position of 'l' back by 1
    print "".join(rword)   ## Use join to bring each item in a list together
    return "".join(rword)
reverse("Python!")

#6

keep reverse_text="" outside the loop


#7

with range() you can also iterate backwards and save yourself a few lines and variables:

    for i in range(len(text)-1, -1, -1):

If len(text) = 10 this will start at 9 (index starts at 0), end at -1 (which == 0 index when counting backward), counting by -1.


#10

You can actually use only str, as the expected results should be a str not a list.

I used the length of the text variable to get the last value, in this case I set the counter to one as I will be deducting this from length to get the last character from the string.

Example:

text = Code

The lenght of "Code" is four, however the highest index is three, reason why I set the counter to one.

counter = 1
text[len(text)-counter]

So with the example should look like this:

text[4-1]

So it will grab the letter "e"

def reverse(text):
    new_word = ""    ## create the variable that will contain the new word.
    counter = 1        ## Counter to use to ge the information with len()
    for n in text:
        new_word = new_word + text[len(text)-counter]
        counter += 1
    return new_word

#11

I agree!
It will be more simple using only str, because we can "add" each character one after another using +.

def reverse(text):
    
    text_reverse = ""
    for char in text:
        text_reverse = char + text_reverse
    return text_reverse

1st line : create function called reverse
2nd line: define a blank str called text_reverse
3dr line : using for loop to run through each character in text

for example: if we call the function using the word, love.

reverse(love)

after the 1 run of the for loop, text_reverse = l
2nd run of the for loop, tex_reverse = o + l =ol
3rd run of the for loop, tex_reverse = v + ol =vol
4th run of the for loop, tex_reverse = e + vol =evol


#12

def reverse(text):
string = ""
for i in range(len(text),0,-1):
string += text[i-1]
return string

print reverse("Hello")


#13

Simple and good code..


#14

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