0

I need input data from keyboard in console Python like this:

 1427  0   

   876652098643267843 
5276538

How can I catch them to array?

3 Answers3

0

Try this:

import fileinput

for line in fileinput.input():
    #your code
    print(line)

This will accept a stream of characters till EOF is encountered. (Cnrl + D)

You may then split the each line to obtain individual values.

astrosyam
  • 857
  • 4
  • 15
0

First you need to read the multiline data in Python. How to to this is described here: raw-input-across-multiple-lines-in-python

Once you have your input in a variable you process it.

Very simple example. Don't know if it meets the requirements from your task:

import math
sentinel = '#' 
result = []
all_data = iter(raw_input, sentinel)
for line in all_data:
    data = line.split(' ')
    for Ai in data:
        Ai = Ai.strip()
        try:
            result.append(math.sqrt(int(Ai)))
        except:
            pass

for i in reversed(result):
    print("%.4f"%i)

Now your task is to optimize.

kotlet schabowy
  • 918
  • 1
  • 7
  • 18
0

If you like to send EOF and have the script process the data, you can use this code:

import math
import sys
result = []
for line in sys.stdin.readlines():
    data = line.split(' ')
    for Ai in data:
        Ai = Ai.strip()
        try:
            result.append(math.sqrt(int(Ai)))
        except:
            pass

for i in reversed(result):
    print("%.4f"%i)

On Windows you need to press CRTL + Z and then ENTER. You will see ^Z in the CMD shell. On Linux press CTRL + D.

kotlet schabowy
  • 918
  • 1
  • 7
  • 18