I need input data from keyboard in console Python like this:
1427 0
876652098643267843
5276538
How can I catch them to array?
I need input data from keyboard in console Python like this:
1427 0
876652098643267843
5276538
How can I catch them to array?
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.
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.
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.