You'll need to split your code into two threads, one which continuously prints and the other which listens for input. When the input listener receives an input it will need to send a message to the printing thread to stop.
import time
import threading
# Create printer function to print output
# make sure you add a lock so the printing doesn't go all funny
def printer(lock):
i = 0
while True:
i += 1
time.sleep(0.2)
with lock:
print(f"i's value is {i}")
# create a thread lock to allow for printing
lock = threading.Lock()
# Create the thread to print
p = threading.Thread(target=printer, args=(lock,), daemon=True)
# start the thread
p.start()
# wait for input and when received stop the thread.
if input():
p.join()