Reverse, string it together


#1

The code works but as you can see I had to import a module/function, (line 1) in order for the end="" statement to work, i e to print or return the reversed string in one line. I cheated for this line. sorry. Could I have done this differently? Thank you

from future import print_function # this is imported becaause codecademy version of python will not accept an 'end="" ' statement
def reverse(greenfly):
result = []
for i in range(len(greenfly)-1,-1,-1):# moves he character occupying the 0 index positon to index -1 (the last) position
print (greenfly[i], end="")
result.append(greenfly[i]) # this allows the printing of the reversed string. The code will be accepted without this line
return "".join(result) #this is to remove the quotation marks between characters

word = "testing this"
reverse(word)

Replace this line with your code.


#2

One supposes that if intermittent printing was required, the suggested code would uphold that requirement, but it is a wee bit moot given that the function returns a reversed string, which can be printed in one statement upon return. Good effort, all the same. Learning new things is part and parcel of every day when we keep our minds open and occupied.


#4

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