7/15 Reverse, SyntaxError



<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.>
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


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>


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?


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.


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


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