Input:
691 41
947 779
Output:
1300
336
I have tried this solution a link
but my problem is end of last line is unknown
My Code:
for line in iter(input,"\n"):
a,b = line.split()
a = int(a)
b = int(b)
result = 2 * (a - b)
print(result)
My code is printing the desired output but the for loop is not terminating. I am a new bee to python is there any way to find the last input from the stdin console???
What are you missing is that input()
strips the newline from the end of the string it returns, so that when you hit <RET>
all alone on a line what iter()
sees (i.e., what it tests to eventually terminate the loop) is not "\n"
but rather the empty string ""
(NB definitely no spaces between the double quotes).
Here I cut&paste an example session that shows you how to define the correct sentinel string (the null string)
In [7]: for line in iter(input, ""):
...: print(line)
...:
asde
asde
In [8]:
As you can see, when I press <RET>
all alone on a n input line, the loop is terminated.
ps: I have seen that gcx11 has posted an equivalent answer (that I've upvoted). I hope that my answer adds a bit of context and shows how it's effectively the right answer.