-1

I've created a few test programs to show what I mean

import os
r = open('in.txt', 'r')
for line in r.readlines():
    print line

Above program prints every line in 'in.txt' which is what I want with the others

for line in raw_input():
    print line

I input "asdf" and it gives me (it also does not let me input multiple lines)

a
s
d
f

Lastly,

for line in str(input()):
    print line

I input "asdf" and it gives me (does not let me input multiple lines)

Traceback (most recent call last):
  File "C:/Python27/test.py", line 1, in <module>
    for line in str(input()):
  File "<string>", line 1, in <module>
NameError: name 'asdf' is not defined

Can someone please tell me what is going on? What is the difference between these 3 input methods other than reading files and standard input?

Adam
  • 11
  • 2

2 Answers2

3

raw_input() takes one line as input from the user and gives a string, and when you loop through with for ... in you're looping through the characters.

input() takes the input and executes it as Python code; you should rarely if ever use it.

(In Python 3, input does the same thing as Python 2's raw_input, and there is not a function like Python 2's input.)

If you want multiline input, try:

lines = []
while True:
    line = raw_input()
    if line == '': break
    lines.append(line)

for line in lines:
    # do stuff
    pass

Input a blank line to signal end of input.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Worth noting that in Python3.x, `raw_input()` has been renamed to `input()` and Python2.x's `input()` has gone the way of the dinosaurs – Adam Smith Jan 02 '14 at 21:51
  • So then how do I do the same thing as the first program but use raw_input() rather than reading a file? – Adam Jan 02 '14 at 21:52
  • @Adam: You don't. `raw_input()` always gives you a **single line**; a file gives you a sequence of lines. – Martijn Pieters Jan 02 '14 at 21:52
  • @Adam How would you tell when to stop reading lines? – tckmn Jan 02 '14 at 21:52
  • @Adam You CAN, but it's more complicated than it's worth. If you need to request many lines of data from your user, you'll need to use a `while` loop to check for some sentinel (usually an empty string) that signifies the end of the data, then a new `raw_input()` that gets appended to a list, which then gets used with `'\n'.join(list_)` after the while loop breaks. – Adam Smith Jan 02 '14 at 21:54
  • @Adam: Did you want to read from `sys.stdin` instead, perhaps? – Martijn Pieters Jan 02 '14 at 21:58
0

Pursuant to your secondary question in Doorknob of Snow's answer, here's sample code but be aware, THIS IS NOT GOOD PRACTICE. For a quick and dirty hack, it works alright.

def multiline_input(prompt):
    """Prompts the user for raw_input() until an empty string is entered,
then returns the results, joined as a string by the newline character"""

    tmp = "string_goes_here" #editor's note: this just inits the variable
    tmp_list = list()
    while tmp:
        tmp_list.append(tmp)
        tmp = raw_input(prompt)
    input_string = '\n'.join(tmp_list[1:])
    return input_string

for line in multiline_input(">> ").splitlines():
    print(line)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112