7/15 Reverse, SyntaxError


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/reverse?action=lesson_resume&link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>
Code works in PyCharm but throws the following error here:
File “python”, line 6
return print(’’.join(str(i) for i in rvrs))
^
SyntaxError: invalid syntax

<What do you expect to happen instead?>
I’ve tried it with strings such as ‘abcde’ and ‘Python!’ and both seem to work in PyCharm but codecademy reports an error

```python

def reverse(text):
org = list(text)
rvrs = []
for c in org:
rvrs.insert(0, c)
return print(’’.join(str(i) for i in rvrs))

<do not remove the three backticks above>

#2

you can’t have return and print at the same line, i don’t understand why you simply join(rvrs) together, why would you need a for loop?


#3

Thanks for the help with the return print, it works now.

My thought process behind the solution was to first convert the string to a list and then add each element of the list at index 0 thusly reversing the list. the join was mainly to convert the list back to a string without any spaces between the characters.


#4

the for loop i was referring to, was the one you used in the same line as .join()

join all handles the list to string conversion for you, so no need for you to use a loop


#5

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