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
  • 20,855
  • 9
  • 60
  • 81

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
  • 55,673
  • 6
  • 74
  • 107