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

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0

    Help with concatenating


    How do I concatenate or get the out put to print on the same line? I know I need a "+," but it's not working where I think it should.

    message = raw_input("Please enter a message to be printed backwards. ")
    print

    i = 1

    while i <= len(message):
    new_message = message[-i]
    i = i + 1
    print new_message

    raw_input("\n\nPress the enter key to exit.")
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    Originally Posted by briangw
    How do I concatenate or get the out put to print on the same line? I know I need a "+," but it's not working where I think it should.

    message = raw_input("Please enter a message to be printed backwards. ")
    print

    i = 1

    while i <= len(message):
    new_message = message[-i]
    i = i + 1
    print new_message

    raw_input("\n\nPress the enter key to exit.")
    Well, there are several ways. An easy way is to put a comma after what you're printing:
    Code:
    print "This is some text",
    This also works great for printing multiple objects of varying type:
    Code:
    print 1, "is greater than", 0, "but less than", 8
    The print with comma syntax will always put a space between your items.

    If you want more control on exactly what you are printing, I suggest you read what Mark Pilgrim has to say in Dive Into Python.
    Last edited by percivall; February 18th, 2004 at 05:54 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    First of all, you need to indent (four spaces) where it says:

    new_message = message[-i]

    You're using the plus sign in the proper way. I think I know why your script is not working, though.

    Take a look at this:

    Code:
    01: message = raw_input("Please enter a message to be printed     backwards. ")
    02: print
    03: 
    04: i = 1
    05: 
    06: while i <= len(message):
    07:     new_message = message[-i]
    08: i = i + 1
    09: print new_message
    10: 
    11: raw_input("\n\nPress the enter key to exit.")
    Okay, look on line four. Now look on line eight. See the problem yet?

    You defined i as 1. You then definied i as i + 1. It's going to use the latest definition, and will forget about the i = 1. I would use:

    i = 1
    i2 = i + 1

    Do you understand what I'm saying?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    I see now. indenting is something I still have not fully grasped. Created a ton of loops with not doing it properly. Thanks for the input. I see the error of my ways!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    Originally Posted by MasterChief
    First of all, you need to indent (four spaces) where it says:

    new_message = message[-i]

    You're using the plus sign in the proper way. I think I know why your script is not working, though.

    Take a look at this:

    Code:
    01: message = raw_input("Please enter a message to be printed     backwards. ")
    02: print
    03: 
    04: i = 1
    05: 
    06: while i <= len(message):
    07:     new_message = message[-i]
    08: i = i + 1
    09: print new_message
    10: 
    11: raw_input("\n\nPress the enter key to exit.")
    Okay, look on line four. Now look on line eight. See the problem yet?

    You defined i as 1. You then definied i as i + 1. It's going to use the latest definition, and will forget about the i = 1. I would use:

    i = 1
    i2 = i + 1

    Do you understand what I'm saying?
    I tried what you said, but it locked up Python. I rebooted and it did the same thing. Here's my modified code:

    message = raw_input("Please enter a message to be printed backwards. ")
    print

    i = 1

    while i <= len(message):
    new_message = message[-i]
    i2 = i + 1
    print new_message

    raw_input("\n\nPress the enter key to exit.")

    Isn't the problem with changing the "i" variable to "i2?"

    Sorry, the formatting here isn't showing right. I do have the new_message line indented 4 spaces.
    Last edited by briangw; February 18th, 2004 at 06:17 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    543
    Rep Power
    24
    Hmmm... I'll have to play around with it a bit. It was locking up on me with the original script and the modified script.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    after putting the comma in at the end of the print statement, it worked.

    what's interesting is that my professor so far has said that he has seen many different ways (and code) to come to the same conclusion.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The reason you're code was locking Python up is because 'i' could never be anything but one so the loop could never end...

    So all you really needed here was the comma GW . But wait, that leaves a space between each letter which isn't very pretty. But we can use the backspace char (\b) to remove it.

    Code:
    #!/usr/bin/env python
    
    message = raw_input("Please enter a message to be printed backwards. ")
    print
    
    i = 1
    
    while i <= len(message):
        new_message = message[-i]
        i = i + 1   
        print '\b' + new_message,
    
    raw_input("\n\nPress the enter key to exit.")
    Of the same thing could be done like this in Python 2.3 using extended slices.

    Code:
    >>> print raw_input('Enter the word to be Reversed: ')[::-1]
    Enter the word to be Reversed: word
    drow
    >>>
    Mark.
    Last edited by netytan; February 18th, 2004 at 07:10 PM.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    No good. He's demanding a "+" in there. He sent me to a chapter on "building a new string" by rebuilding the word, but backwards with no spaces.

    My problem is is that I'm confused with how the book is explaining it
    Last edited by briangw; February 18th, 2004 at 07:24 PM.
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Very simple answer, loop over the string in reverse and concatenate each letter to a new string.. leaving you with a string and all using the + operator.

    Code:
    >>> string = raw_input('Enter the string to be reversed: ')
    >>> output = ''
    >>> for letter in string[::-1]:
    ...     output = output + letter
    ...     
    >>> print output
    gnirts
    >>>
    If you're not aloud to use extended slices then you'll neet to loop over the number of letters (for each in range(len(string))... or... for index, value in enumerate(string)) and offset the index number by the strings len(). If you want an example let me know?

    Also, do you have a link to this book, maybe i can help you understand it .

    Mark.
    programming language development: www.netytan.com Hula

  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    Originally Posted by netytan
    Very simple answer, loop over the string in reverse and concatenate each letter to a new string.. leaving you with a string and all using the + operator.

    Code:
    >>> string = raw_input('Enter the string to be reversed: ')
    >>> output = ''
    >>> for letter in string[::-1]:
    ...     output = output + letter
    ...     
    >>> print output
    gnirts
    >>>
    If you're not aloud to use extended slices then you'll neet to loop over the number of letters (for each in range(len(string))... or... for index, value in enumerate(string)) and offset the index number by the strings len(). If you want an example let me know?

    Also, do you have a link to this book, maybe i can help you understand it .

    Mark.
    Python Programming for the Absolute Beginner, by Michael Dawson, 1-59200-073-8

    I tried your code, but got error:

    Traceback (most recent call last):
    File "C:/Documents and Settings/Brian/Desktop/PYTHON/Class 5/test", line 9, in ?
    for letter in message[::-1]:
    TypeError: sequence index must be integer
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    17
    Rep Power
    0
    Got it!

    message = raw_input("Please enter a message to be printed backwards. ")
    new_message = ""



    for letter in message:
    ****new_message = letter + new_message
    print new_message

    raw_input("\n\nPress the enter key to exit.")

    I hate programming. It's required for my network admin degree. Thanks for your help. Midterms are coming up next week and we have some programs to write, so I may be calling on some help again.

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo