The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Append() and raw_input()
Discuss Append() and raw_input() in the Python Programming forum on Dev Shed. Append() and raw_input() Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 29th, 2012, 07:53 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 32 m 20 sec
Reputation 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
|

December 29th, 2012, 09:19 AM
|
 |
Contributing User
|
|
|
|
|
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!
|

December 29th, 2012, 10:21 AM
|
 |
Contributing User
|
|
|
|
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
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
Last edited by Dietrich : December 29th, 2012 at 11:21 AM.
|

December 29th, 2012, 11:05 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 74
  
Time spent in forums: 1 Day 2 h 22 m 37 sec
Reputation Power: 2
|
|
|
This looks like the result of using extend() instead of append(). Is the code you wrote here copied verbatim from what you actually ran?
|

December 29th, 2012, 11:25 AM
|
 |
Contributing User
|
|
|
|
Quote: | 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']
'''
|

December 30th, 2012, 06:26 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 32 m 20 sec
Reputation 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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|