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?

asked Jan 15, 2016 at 21:27
6
  • 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. Commented Jan 15, 2016 at 21:30
  • 1
    Please paste your code into your question. Don't use pictures for code. Commented Jan 15, 2016 at 21:30
  • 2
    The images are the same. Could you add your code to the question? Commented Jan 15, 2016 at 21:30
  • Possible duplicate of How can I reverse a list in python? Commented Jan 15, 2016 at 21:54
  • Is it homework? Please notice that Python in NOT C/C++ language. To reverse a text consider: "text"[::-1] => "txet". Commented Jan 15, 2016 at 22:18

1 Answer 1

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.

answered Jan 15, 2016 at 21:34
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.