#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    11

    help with this code


    can anybody explain me what is wrong i am doing exercises from a python book and i am supoced to write a program that spells backwards the user input, i already did more complex pieces of programs but i dont get the problem with this one
    thanks


    Code:
    #this program is supoced to invert the message the user inputs
    
    message = raw_input("Please enter a message: ")
    char = int(len(message))
    new_msg = ""
    terminate = "no"
    
    while char >= 0:
    	new_msg += message[char]
    	char -= 1
    
    print "Your message inverted is: ", new_msg
    raw_input("enter to exit")
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Let's say message is "hello".
    Code:
    ...
    char = int(len(message))   # -> char = 5
    ...
    new_msg += message[char]
    There is no element message[5], so you get an error (inizes start at 0). You could do it like this:
    Code:
    char = int(len(message))-1
    but to simply reverse a string you can write
    Code:
    new_msg = message[::-1]
    Regards,
    mawe
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    11
    thanks man!

IMN logo majestic logo threadwatch logo seochat tools logo