0
def main():
    print(reverseString("Hello"))

def reverseString(string):
    newString=""
    for i in range(len(string)-1,-1):
        newString+=string[i]
    print newString

main()

I tried running this code but nothing is printed and I don't know what the problem is.

1
  • I'm assuming this is a school assignment, or other sort of exercise. But if you want to know a quick way to reverse a string: print( string[::-1] ) Commented Feb 23, 2020 at 1:05

3 Answers 3

2

This is missing the step of -1 in the range():

for i in range(len(string)-1, -1, -1):

Without the step the for loop immediately exits, leaving newstring as ''.

BTW: you are not returning anything from reverseString() so:

print(reverseString("Hello"))

Will print None, which I assume is not wanted. You probably want to:

return newString

in reverseString().

Sign up to request clarification or add additional context in comments.

Comments

0

Because your reverseString method doesnt have a return value. try removing print in your main method.

Comments

-1

Try this:

def main():
    print(reverseString("Hello"))


def reverseString(string):
    newString=""
    for i in range(len(string)):
        newString+=string[len(string)-i-1]
    return newString

main()

Comments

Your Answer

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.