Reverse


#1

hey friends

Why my code given an error ? "Does your reverse function take exactly one argument (a string)? Your code threw a "sequence item 0: expected string, int found" error."

def reverse(text):
y = []
for x in range(len(text)-1):
y.append(len(text)-1)

return "".join(y)

Thank you


#2

Try to put some format to your code so we can check your indents and stuff :slight_smile:

A few comments:
-You never used the x element. Is that correct?
-What are you appending exactly the way you expressed it? If the length of the text is always the same, why would it change or move to the next letter in the next cycle?
-You should consider using range but in other way, telling it to loop trough the text but backwards. Hint: range([start], stop, [step])


#3

Using an empty list in this exercise will not suffice because you are required to return a string not a list,

so this should be an empty string,

Here,

there is no need for the -1

Also I suggest using the += operator and not the .append() method,

y +=

on that same line you have to now reverse the text, which you can do by adding the length of the argument minus one, minus the empty for loop variable,

y += text[len(text)-1-x]

Then simply return y,

return y


#4

It worked guys, thanks, happy new year


#5

def reverse(str):
c = len(str)
str2 = ""
for i in range(0,c):
str2 += str2.join(str[c-i-1])
return str2


#6

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