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

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Append() and raw_input()


    Hi, I have not been doing python for that long so please bear with me. Here is an example of the problem I am having:

    list = []
    input = raw_input()
    list.append(input)
    print list


    when I print "list" I get the individual letters I typed when assigning "input" like this:

    ['i, n, p, u, t']

    why isn't is giving me the whole word?

    thanks in advance
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,708
    Rep Power
    480
    I've put your program into file p.py, and on the command line I execute, `hmmph' my input is shown

    $ python p.py
    hmmph
    ['hmmph']
    $

    I don't comprehend the problem. raw_input returns the keyboard input without the new line as a string.


    1) Hiding the names of builtin functions is usually bad practice. You hid both `input' and `list'.

    2) If still confused please show the input you provide and the output you expect. I think explaining what you think python does is counterproductive, because it's not yet correct.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Getting ['i, n, p, u, t'] doesn't make any sense, unless
    you entered the string i, n, p, u, t

    Avoid using Python function names as variable names.
    When in doubt prefix with my like this:
    Code:
    mylist = []
    myinput = raw_input()
    mylist.append(myinput)
    print mylist
    Last edited by Dietrich; December 29th, 2012 at 11:21 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    This looks like the result of using extend() instead of append(). Is the code you wrote here copied verbatim from what you actually ran?
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Originally Posted by Nyktos
    This looks like the result of using extend() instead of append(). Is the code you wrote here copied verbatim from what you actually ran?
    Sharp mind Nyktos!
    Code:
    mylist = []
    myinput = raw_input()
    mylist.extend(myinput)
    print mylist
    
    ''' result typing in the word input -->
    ['i', 'n', 'p', 'u', 't']
    '''
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    sorry, when I was messing around with something else I must have tried extend() and not changed it. I have changed it now and it works, thanks for the help!

IMN logo majestic logo threadwatch logo seochat tools logo