I am working pyhton on codecademy and I stucked in one part. The goal is this: "Define a function called reverse that takes a string 'text' and returns that string in reverse. You may not use reversed or [::-1] to help you with this." I did this one and it is not working like this:
t = raw_input("Enter: ")
def reverse(t):
x = []
for a in range(len(t)):
x.append(t[len(t) - a - 1])
print ''.join(x)
but when I do it like this it is working.
t = raw_input("Enter: ")
x = []
for a in range(len(t)):
x.append(t[len(t) - a - 1])
print ''.join(x)
what is wrong with the first one?
asked Jan 15, 2016 at 21:27
Bahadır
5121 gold badge4 silver badges8 bronze badges
1 Answer 1
The first does not work because, you're not calling your reverse function on t.
def reverse(t):
x = []
for a in range(len(t)):
x.append(t[len(t)-a-1])
return ''.join(x)
t = raw_input("Enter: ")
print(reverse(t))
In your example you're obtaining the input, but doing nothing with it.
answered Jan 15, 2016 at 21:34
jacob
4,9871 gold badge25 silver badges35 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
"text"[::-1]=>"txet".