Bahadır
Bahadır

Reputation: 502

Functions with strings

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?

Upvotes: 1

Views: 104

Answers (1)

jacob
jacob

Reputation: 4956

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.

Upvotes: 2

Related Questions