Is it possible to continue reading input on sys.stdin
? I'm trying to continuously execute a function based on input. (My function only prints to sys.stdout
and sys.stderr
, and doesn't return anything.) My Code:
def prime(number):
number = abs(int(number))
for i in range(3, number):
if number % i == 0:
print("Not Prime!", file = sys.stderr, flush = True)
sys.exit(0)
print("Prime!", file = sys.stdout, flush = True)
for i in sys.stdin:
prime(i)
Here's the output:
>>> for i in sys.stdin:
prime(i)
43 # Input
Prime! # In sys.stdout and it continues reading from sys.stdin
55 # Input
Not Prime! # In sys.stderr and stops.
>>>
I've tested it out and determined that it continues reading when the output of my function is on sys.stdout
and stops when outputting on sys.stderr
.