Search code examples
pythonio

Reading input lines for int objects separated with whitespace?


I'm trying to solve a programming problem that involves returning a boolean for an uploaded profile pic, matching its resolution with the one that I provide as input and returning a statement that I've described below. This is one such test case that is giving me errors:

180
3
640 480      CROP IT
320 200      UPLOAD ANOTHER
180 180      ACCEPTED

The first line reads the dimension that needs to be matched, the second line represents the number of test cases and the rest comprise of resolutions with whitespace separators. For each of the resolutions, the output shown for each line needs to be printed.

I've tried this, since it was the most natural thing I could think of and being very new to Python I/O:

from sys import stdin, stdout

dim = int(input())
n = int(input())
out = ''

for cases in range(0, n):
    in1 = int(stdin.readline().rstrip('\s'))
    in2 = int(stdin.readline().rstrip('\s'))

    out += str(prof_pic(in1, in2, dim))+'\n'

stdout.write(out)
ValueError: invalid literal for int() with base 10 : '640 480\n'

prof_pic is the function that I'm abstaining from describing here to prevent the post getting too long. But I've written in such a way that the width and height params both get compared with dim and return an output. The problem is with reading those lines. What is the best way to read such lines with differing separators?


Solution

  • You can try this it is in python 3.x dimention=int(input()) t=int(input()) for i in range(t): a=list(map(int,input().split()))