Yahoo Poland Wyszukiwanie w Internecie

Search results

  1. 1 lis 2012 · 20 Answers. Sorted by: 193. The easiest way is to just interrupt it with the usual Ctrl-C (SIGINT). try: while True: do_something () except KeyboardInterrupt: pass. Since Ctrl-C causes KeyboardInterrupt to be raised, just catch it outside the loop and ignore it. edited Nov 1, 2012 at 16:18. SilentGhost. 318k67310293.

  2. 3 lut 2016 · For instance, Windows has Pywin32. Example implementation: import time import win32api def is_pressed(key): x = win32api.GetKeyState(key) return (x & (1 << 8)) != 0 print "Beginning calculation. Press the Q key to quit." while not is_pressed(ord("Q")): print "calculating..." time.sleep(0.1) print "Finished calculation."

  3. 6 mar 2024 · Kill a While Loop with a Keystroke Using KeyboardInterrupt. In this example, below code a variable num with 11 and enters an infinite loop, printing and incrementing num by 2 in each iteration until an even number is encountered. The loop is slowed down by one second.

  4. try: while True: do_something() except KeyboardInterrupt: pass. For this the exit keystroke would be ctrl+c. Or if you want to use a module you can take a look at the Keyboard module and use the keyboard.on_press(<your_quit_key>) while True: # Do your stuff.

  5. 18 sie 2023 · Contents. Basic syntax of while loops in Python. Break a while loop: break. Continue to the next iteration: continue. Execute code after normal termination: else. Infinite loop with while statement: while True: Stop the infinite loop using keyboard interrupt (ctrl + c) Forced termination. See the following article for information on a for loop.

  6. 10 gru 2023 · If you just run the command-line Python REPL (started by running python or python3 without arguments), you will be able to break out “immediately” of such a tight loop by hitting Ctrl+C, but when running IDLE, it seems to be practically impossible.

  7. 28 kwi 2024 · Terminating a while loop with a keystroke in Python 3 can be achieved using different approaches depending on the platform. By utilizing modules like msvcrt or sys , we can detect user input and break out of the loop based on specific key presses.

  1. Ludzie szukają również