Reverse


#1

<In what way does your code behave incorrectly? Include ALL error messages.>
I keep getting an error message and can’t figure out what is wrong. Here’s the message:
Oops, try again. Does your reverse function take exactly one argument (a string)? Your code threw a “‘str’ object is not callable” error.

Any ideas?

Here’s my code:

def reverse(text):
    length = len(text)
    new_word = []
    while length >= 0:
        new_word.append(text(length))
        length -= 1
    else:
        return ''.join(new_word)   



#2

you use brackets, not parentheses, to access a character in a String at a specific index.
So it should be new_word.append(text[length])


#3

also getting “Oops, try again. Your code looks a bit off–it threw a “string index out of range” error. Check the Hint if you need help!”


#4

oh sorry, I should have caught that before, you need to start at length - 1 since index starts at 0. if its a 5 letter word the 5th letter is actually at index 4


#5

def reverse(text):
rev =[]
for i in range(0,len(text)):
i = len(text)-(i+1)
#print text[i]
rev.append(text[i])
return ‘’.join(rev)
#this is the simplest way to get reverse…thanks


#6

def reverse(text):
length = len(text) - 1
new_word = []
for i in text:

    if length >= 0:
        new_word.append(text[length])
        length -= 1

return ''.join(new_word)

print(reverse(“abcd”))

i think this is working also


#7

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