I recently participated in hackathon for the first time and got stuck on the first problem. I solved the algorithm, but couldn't figure out how to take values from stdin using Python. This is the question:
There are two college students that want to room together in a dorm. There are rooms of various sizes in the dormitory. Some rooms can accomodate two additional students while others cannot.
Input: the first input line will be a number n (1 ≤ n ≤ 100), which is the total number of rooms in the dorm. There will be n lines following this, where each line contains two numbers, p and q (0 ≤ p ≤ q ≤ 100). P is the number students already in the room, while q is the maximum number of students that can live in the room.
Output: print the number of rooms that the two students can live in.
This is my solution. I've tested it using raw_input() and it works perfectly on my interpreter, but when I change it to just input() I get an error message.
def calcRooms(p, q):
availrooms = 0
if q - p >= 2:
availrooms += 1
return availrooms
def main():
totalrooms = 0
input_list = []
n = int(input())
print n
while n > 0:
inputln = input().split() #accepts 2 numbers from each line separated by whitespace.
p = int(inputln[0])
q = int(inputln[1])
totalrooms += calcRooms(p, q)
n -= 1
return totalrooms
print main()
The error message:
SyntaxError: unexpected EOF while parsing
How do I accept data correctly from stdin?