# Practice makes perfect/7 Reverse: Is this an acceptable answer?

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
The code I’m presenting does not show any errors. It returns the string in reverse as per the instructions.

<What do you expect to happen instead?>
I would like to know if my programatic solution is acceptable or if I oversimplified the solution.

I am aware that the instructions did indicate that we should not use reversed or [::-1].
Yes, I used -1 but in the context of indicating a starting point with indexes.
Without the -1 how can we start at the end or backwards?

If I did not provide the correct solution, even if my function properly returns the string backwards, please help me better understand what is expected as an acceptable answer.

Thank you!

``````def reverse(text):
new_word = []
for i in text[len(text)::-1]:
new_word.append(i)
new_word = " ".join(new_word)
return new_word
``````

#2

While yes, it is a good solution, it is not what is wanted for this exercise. Besides, if you could use `[::-1]` All you’d have to do is…

``````def reverse(text):
return text[::-1]
``````

Without using `[::-1]`, what other options do we have?

Well, we can make an empty list, loop in reverse of the string using `range()`, append to the list, and then use `.join()` to join the list into a string after the loop has finished running.

#3

Thank you for the tips!

#4

Would this be a legitimate solution as per what is expected in the exercise?

``````def reverse(text):
new_word = []
for i in range(len(text)-1,-1,-1):
new_word.append(text[i])
new_word = " ".join(new_word)
return new_word
``````

Also, this is somewhat in parallel to the original question, why is it that indenting the penultimate line causes this error:

AttributeError: ‘str’ object has no attribute ‘append’

``````def reverse(text):
new_word = []
for i in range(len(text)-1,-1,-1):
new_word.append(text[i])
new_word = " ".join(new_word)
return new_word
``````

Where could I learn more about proper indentation in Python, tab vs space etc. as well as more about objects and attributes in the context of Python programming? Is that covered at some point in the Codecademy Python course?

#5

Yes, I do believe it would. However, if you wanted to, you could make it shorter by doing something like this:

``````def reverse(text):
new_word = []
for i in range(len(text)-1,-1,-1):
new_word.append(text[i])
return " ".join(new_word)
``````

Indenting `new_word = " ".join(new_word)` so that it is in the for loop means that it executes in each iteration of the loop. In doing so you would be reassigning `new_word` to a string before the loop is done appending items to it. Since `.append()` only works for arrays, it returns an error.

Yes, yes it is. However, if you would like to learn more, you can always check out the documentation.

#7

Thank you once again for a great answer!

#8

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