简体   繁体   中英

What button should be pressed after entering arguments with sys.stdin.read() in python

Iam working in a simple python code that uses sys library to get a multi-lines input through cmd, I am using sys.stdin.read() to enter my input like that:

3 50
60 20
100 50
120 30

then i don't know what to press to run the program.

i tried pressing enter, Ctrl+D and Ctrl+z Nothing happened

#Uses python3
import sys

def max_dot_product(a, b):
    a = sorted(a)
    b = sorted(b)
    res = 0
    for i in range(len(a)):
        res += a[i] * b[i]
    return res

if __name__ == '__main__':
    input = sys.stdin.read()
    data = list(input.split())
    n = data[0]
    a = data[1:(n + 1)]
    b = data[(n + 1):]
    print(max_dot_product(a, b))

The input works fine for me; I used enter (optional) and ctrl-D after the last pair of values.

From there, the program fails because you try to multiply strings. You have to convert the input to numeric.

data = list(map(int, input.split()))

Output:

3 50
60 20
100 50
120 30
[3, 50, 60, 20, 100, 50, 120, 30]
9100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM