Not sure why Python readlines() doesn't work here
<p>Hi I have a block of code here, intending to read the input into variables:</p>

<pre><code>if __name__ == "__main__":
    data = list(map(int, sys.stdin.readlines().split()))
    n, capacity = data[0:2]
    values = data[2:(2 * n + 2):2]
    weights = data[3:(2 * n + 2):2]
    opt_value = get_optimal_value(capacity, weights, values)
    print("{:.10f}".format(opt_value))
</code></pre>

<p>Then the self-defined get_optimal_value(capacity, weights, values) will be executed. Input example:</p>

<pre><code>3 50
60 20
100 50
120 30
</code></pre>

<p>It is supposed to achieve the following:</p>

<pre><code>n = 3
capacity = 50
values = [60,100,120]
weights = [20,50,30]
</code></pre>

<p>However, the code doesn't proceed after I type in the above input numbers and gives me no result. That means the numbers are not read into the variables and lists are not formulated. I tried my code into an automated error checker; it tells me: </p>

<pre><code>AttributeError: 'list' object has no attribute 'split'
</code></pre>

<p>Why is this and how should I modify my code?</p>

<p>Additional question: does the number reading automatically knows where to stop, i.e. when it detects space and no following numbers? I assume the code will execute the function and calculate the result right after it reads all the input data, is that correct in terms of process? I feel like lacking a 'step' to notify the code to 'calculate' after typing in the inputs. </p>