3

With this code:

A = raw_input("Please input text here")
print A
print len(A)

For the input, when I copy and paste from notepad, it only recognizes the first line of the following:

Cars are fast
Motorcycles are faster
Planes are even faster

Therefore, print A will print 'Cars are fast' and print len(A) with print 13, which is the length of 'Cars are fast'.

How can I get Python to recognize the remaining lines of my input?

Thank you.

anon582847382
  • 18,791
  • 5
  • 51
  • 56
user3422147
  • 71
  • 1
  • 4

3 Answers3

5

raw_input will read one line of input only.

See answers to this question for a general overview of getting input from the command line.

If you want to use raw_input then a loop will read all your lines but you will need a way to break out, something like this.

while True:
    A = raw_input("Please input text here (Q to quit)")
    if len(A) == 1 and A[0].lower() == "q":
        break
    print A
    print len(A)

To collate multiple lines do something like this

data = []
while True:
    A = raw_input("Please input text here (Q to quit)")
    if len(A) == 1 and A[0].lower() == "q":
        break
    data.append(A)

print data
for A in data:
    print len(A)

Remember to enter a newline after pasting. Also, the raw_input prompt messages may not display correctly.

You could go crazy and manage the prompt. Expecting a zero length input means the user is trying to quit.

data = []
prompt = "Please input text (Q to quit):\n"
while True:
    if data:
        A = raw_input()
    else:
        A = raw_input(prompt)    
    while len(A) == 0:
        A = raw_input(prompt)
    if len(A) == 1 and A[0].lower() == "q":
        break        
    data.append(A)

for A in data:
    print "%s - %i" % (A, len(A))
Community
  • 1
  • 1
Graeme Stuart
  • 5,277
  • 1
  • 23
  • 43
  • is there a different input function that will read multiple lines? – user3422147 Mar 16 '14 at 17:33
  • No, but you can write your own in the suggested form. – Graeme Stuart Mar 16 '14 at 17:35
  • thanks for taking the time to reply. I might be wrong but the way that you're suggesting I do it would require me to manually enter each line of text wouldn't it? What I would like to do is input any number of lines, and then have python return a list of strings so that list[0] would be the first line, list[1] would be the second line, etc. – user3422147 Mar 16 '14 at 17:45
  • You can copy and paste multiple lines at once and the programme will read each line in turn. You might want to collect all the data into a list before processing it. – Graeme Stuart Mar 16 '14 at 17:47
  • A small deviation from this could be using '' (a blank string) as a sentinel instead of 'q', it all depends upon your requirement. But AFAIK Python doesn't have a inbuilt multi line read. – Jagjeet Singh Thukral Mar 16 '14 at 18:05
  • Of course @JagjeetSinghThukral is correct, the 'Q' is optional and depends on what you need. – Graeme Stuart Mar 16 '14 at 18:11
0

There is sys.stdin object that has read method. By default method reads till end of file.

>>> import sys
>>> file_content = sys.stdin.read() # press CTRL+D to close stdin

You can read more about this method using:

>>> help(sys.stdin.read)

Or

>>> help(sys.read)
aisbaa
  • 8,774
  • 5
  • 31
  • 43
0

Thanks Graeme for the help, you fixed my problem.

To anybody that can help me understand this, the one thing I can't understand is why the code below loops through all lines in the input. The way I am reading it the code would just infinitely append the first line of the input to data.

data = []

while True:

    A = raw_input("Please input text here (Q to quit)")
    if len(A) == 1 and A[0].lower() == "q":
        break
    data.append(A)

print data
for A in data:
    print len(A)

Thanks once again.

elixenide
  • 43,445
  • 14
  • 72
  • 97
user3422147
  • 71
  • 1
  • 4
  • Generally, you shouldn't ask questions in an answer. That said, the answer to your question is this: it loops as long as `True` is true, which is forever, *but* it breaks out of the loop when the user input is "Q" or "q". – elixenide Mar 16 '14 at 21:59
  • thanks for your tine. I understand that True will always be true and that it breaks when the user inputs q, but what I don't understand is how the functions knows that it is supposed to move down a line every time that it does a loop. I'm not seeing what part of the code tells the function to move to the next line. – user3422147 Mar 17 '14 at 00:09
  • I'm not sure what you mean. [`raw_input` reads a full line of input each time it's called](http://docs.python.org/2.7/library/functions.html#raw_input). – elixenide Mar 17 '14 at 00:37