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. 6 mar 2024 · Below are some of the methods to kill a While Loop with a keystroke in Python: Using KeyboardInterrupt. Using keyboard Library. Utilizing msvcrt Module. Kill a While Loop with a Keystroke Using KeyboardInterrupt.

  3. 3 lut 2016 · try: while x!=5: if x<5: x=x+1. print ('Your value is now %s'%x) if x==5: print('All done, your value is 5') elif x>5: x=x-1.

  4. 9 lip 2021 · To end a while loop prematurely in Python, press CTRL-C while your program is stuck in the loop. This will raise a KeyboardInterrupt error that terminates the whole program. To avoid termination, enclose the while loop in a try/except block and catch the KeyboardInterrupt.

  5. You can use the keyboard module. Note that on Linux you have to run your program as root, though. import keyboard. x = True. while x: print("While loop is on...") if keyboard.is_pressed("c"): x = False. print("While loop is off...")

  6. 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.

  7. 21 gru 2021 · 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. if keyboard.is_pressed("q"): # Key was pressed. break.

  1. Ludzie szukają również