Search code examples
pythoninputraw-input

How to read input in python using raw_input


Sample Input (in Plaintext) 
1 
1 2

Description- The first line contains an integer t, denoting the number of test cases. Next t lines contain two integers, a and b separated by a space.

My question is how can I load these inputs into variables using Python 2.7? Also I need to load this into IDE without using "xxx.py > input.txt"


Solution

  • raw_input() takes in a line of input as a string. The .split() string method produces a list of the whitespace-separated words in a string. int() parses a string as an int. So putting that together, you can read the input you want using

    t = int(raw_input())
    cases = []
    for i in range(t):
        cases.append( map(int, raw_input().split()) )
    

    This will give you a list of a,b pairs if the input is in the correct format, but won't do any error checking.