Thread: Noob help

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

    Join Date
    Sep 2005
    Posts
    60
    Rep Power
    10

    Noob help


    So i went and bought a python book.
    Im at that embarrassing "hello world" stage.

    After each chapter, there are some challenge problems, and even though i can get this one done, i have a question about it.

    I have to get an input from the user and print the message backwards. Which i did, but there's a space between each letter and i don't know why.

    here's the code:


    Code:
    message = raw_input("Enter message: ")
    length = len(message)
    
    
    for i in range(length):
        length = length - 1
        print message[length],
    can anyone tell me why?

    for example, my input is "HI"
    and it prints out "I H"...with a space between letters.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by play_
    So i went and bought a python book.
    Im at that embarrassing "hello world" stage.

    After each chapter, there are some challenge problems, and even though i can get this one done, i have a question about it.

    I have to get an input from the user and print the message backwards. Which i did, but there's a space between each letter and i don't know why.

    here's the code:


    Code:
    message = raw_input("Enter message: ")
    length = len(message)
    
    
    for i in range(length):
        length = length - 1
        print message[length],
    can anyone tell me why?

    for example, my input is "HI"
    and it prints out "I H"...with a space between letters.
    The comma in the print ensures that a space is placed between the items. The best way to do this would be to reverse the string first.

    python Code:
     
    >>> [1, 2, 3][::-1]
    [3, 2, 1]
    >>> "hello"[::-1]
    'olleh'
    >>>


    If you haven't done about slices yet then you could do the same thing manually using a loop a list and the insert.
    python Code:
     
    >>> string = "hello"
    >>> ''.join(string[i] for i in range(len(string) - 1, -1, -1))
    'olleh'
    >>>


    Enjoy,

    Mark.

    Comments on this post

    • CountVikernes agrees : Damn pythonic stuff. *grin* I have to get into that.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    60
    Rep Power
    10
    I see. thank you.

    Also, is a function similar to the switch function in php?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Just to comment a bit further, the print statement always puts a new line after printing something. Which you've probably found, and that's why the comma is there - because that supresses the new line. Except, it does still put a space.

    This behaviour is fine for most "print"ing, but sometimes it's not what you want.

    You can write directly to the output instead, if you want - it would look something like this:

    [code=Python]
    import sys

    message = raw_input("Enter message: ")
    length = len(message)


    for i in range(length):
    length = length - 1
    sys.stdout.write(message[length])
    [/code]


    (But that will probably only work if you save the script and run it - it might not work in some interactive interpreter, because they may already be working directly with the IO )

    Also, you will want to check the "reversed()" function at some point, but maybe not right now.

    Also, is a function similar to the switch function in php?
    If it's like switch in C, no. The accepted way is just to write:

    Code:
    if a==1:
        print "A is 1"
    elif a == 2:
        print "A is 2"
    And add more "elif"'s as necessary.

    Comments on this post

    • netytan agrees
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by play_
    I see. thank you.

    Also, is a function similar to the switch function in php?
    I'm not sure I totally understand what your asking, in PHP the only switch I know of is a statement not a function (indeed it would be more powerful if it were a function in lisp/scheme we have a case function).

    If you're wanting an equivalent statement in Python then there isn't one. You can role your own using dictionaries but it's not pretty and depends on what you want to use the switch for.

    Use the if-elif-else statement.

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

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    60
    Rep Power
    10
    sfb,
    You threw some new stuff there that i will have to look into, thanks, ill look into that.

    and mark,
    yes, i meant statement, not function, sorry.
    but yea...i guess ill just use a bunh of elifs then

IMN logo majestic logo threadwatch logo seochat tools logo