Search code examples
python-2.7stdoutstdin

Dynamically use both python raw_input and raw_input().split()


The input format is:

6
1
2 5
2 7
2 9
1
1

Input:

First line contains an integer Q, the number of queries. Q lines follow. A Type-1 ( Customer) Query, is indicated by a single integer 1 in the line. A Type-2 ( Chef) Query, is indicated by two space separated integers 2 and C (cost of the package prepared) .

I want to read the input from stdin console and here is my code

n = int(input())

stack1 = []
for i in range(n):
    x = input()
    x = int(x)
if x == 2:
    y = input()
    stack1.append(y)
elif x == 1:
    length = len(stack1)
    if length > 0:
        print(stack1.pop())
    else:
        print("No Food")

I have tried x,y = raw_input().split() this statement also fails because sometimes input has single value. Let us know how to read the defined input from stdin ???


Solution

  • Use len() to find length of string based on that change your stdin.

    n = int(input())
    for i in range(n):
        s = input()
        if(len(s) > 1):
            x,y = s.split()
            x = int(x)
        else:
            x = int(s)
        print(x)
    

    Cheers.