[Tutor] Backwards message program
bob gailer
bgailer at gmail.com
Mon Feb 6 20:55:07 CET 2012
On 2/6/2012 2:05 PM, myles broomes wrote:
> 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
>
> print(backw)
> 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
1 - message never changes. Why did you think letters were "taken" from
it? They are copied.
2 - comparing an integer to a string is always False
--
Bob Gailer
919-636-4239
Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120206/c36c83ad/attachment.html>
More information about the Tutor
mailing list