A question on how to read Python input

Hello I’m new to USACO, and I’m using the training website in China.
For questions like milking cows and beads, I can understand the question and the solutions online that I’ve searched, except for the parts where they analyze the input.

Is there some quick and easy way for reading the input in stdin with python?

As an example, the input below is needed:
500
200
200
0

Suggest that 500 is the number of apples I bought, let it be n
200 and 200 is the number of apples each of two my friends ate, let them be y[0] and y[1]
0 is how much I ate, let it be x
The question asks for how many apples are left.
The algorithm for this is quite easy,
I mean, normally it would be:

y = []
n = int(input())
for i in range(2):
    y[i] = int(input())
x = int(input())

(it could be shortened)

How is this done in stdin fashion?

allInputs = input().split(" ")

y = []
n = int(allInputs[0])
for i in range(2):
 y[i] = int(allInputs[1+i])
x = int(allInputs[4])

Maybe a system like this, get all the input in one line?

500 200 200 0
1 Like

If they are all in one line, you can do the following:

total, f1, f2, me = [int(x) for x in input().split()]

OR

total, f1, f2, me = list(map(int, input().split()))

These are my two favorite ways do do this.

1 Like