[Tutor] Backwards message program

myles broomes mylesbroomes at hotmail.co.uk
Mon Feb 6 20:05:56 CET 2012

Im trying to code a program where the user enters a message and it is returned backwards. Here is my code so far:

message = input("Enter your message: ")
backw = ""
counter = len(message)
while message != 0:
    backw += message[counter-1]
    counter -= 1
input("\nPress enter to exit...")

I run the program, type in my message but get back the error code:
'IndexError: String out of range'
I was thinking that maybe the problem is that each time a letter is taken from 'message' and added to 'backw', the length of message becomes a letter shorter but for whatever reason the variable 'counter' doesnt change when its supposed to be minused by 1 letter so its always bigger than message if that makes sense. Any help is much appreciated and thanks in advance.

Myles Broomes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120206/ffad706d/attachment.html>

More information about the Tutor mailing list