1

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?

Bahadır
  • 454
  • 1
  • 4
  • 8
  • 2
    Welcome to StackOverflow! Some users are unable to follow links to images, so if you want your question to be answered, please copy and paste your code into your question. Thank you. – OneCricketeer Jan 15 '16 at 21:30
  • 1
    Please paste your code into your question. Don't use pictures for code. – Mike Müller Jan 15 '16 at 21:30
  • 2
    The images are the same. Could you add your code to the question? – gnerkus Jan 15 '16 at 21:30
  • Possible duplicate of [How can I reverse a list in python?](http://stackoverflow.com/questions/3940128/how-can-i-reverse-a-list-in-python) – kylieCatt Jan 15 '16 at 21:54
  • Is it homework? Please notice that Python in NOT C/C++ language. To reverse a text consider: `"text"[::-1]` => `"txet"`. – Laurent LAPORTE Jan 15 '16 at 22:18
  • In the first one? Are you calling reverse (and if so, are you getting a response) – Foon Jan 15 '16 at 22:25

1 Answers1

2

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.

jacob
  • 4,656
  • 1
  • 23
  • 32