1

In the following program

import itertools
for i in itertools.count():
    print (i)

I wanted to stop this event using keyboard press events.

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82

1 Answers1

1

You can wait for a KeyboardInterrupt exception and then get out:

import itertools

try:
    # Stay inside the loop until Ctrl+C is pressed
    for i in itertools.count():
        print (i)
except KeyboardInterrupt:
    pass
# Go on...
jdehesa
  • 58,456
  • 7
  • 77
  • 121