How to Kill a While Loop with a Keystroke in Python? Last Updated : 29 Apr, 2025 Comments Improve Suggest changes Like Article Like Report While loops are used to repeatedly execute a block of code until a condition is met. But what if you want the user to stop the loop manually, say by pressing a key? Then you can do so by pressing a key. This article covers three simple ways to break a while loop using a keystroke:Using KeyboardInterruptUsing keyboard LibraryUtilizing msvcrt ModuleLet's discuss them one by one:Method 1: Using KeyboardInterrupt (Ctrl+C)This method allows you to manually stop the loop by pressing Ctrl+C. Python import time try: num = 11 while True: if num % 2 == 0: break print(num) num = num + 2 time.sleep(1) # Wait 1 second before next iteration except KeyboardInterrupt: pass print("Continuing with the program") Output:11131517Continuing with the programExplanation:try...except KeyboardInterrupt catches manual interruption (Ctrl+C).loop prints odd numbers and stops once an even number is found or interrupted.Method 2: Using the keyboard LibraryThis approach allows the user to press a specific key (like q) to exit the loop. But to implement this in our code we need to first install the keyboard library using this command:pip install keyboardExample: Python import keyboard def main_loop(): while True: print("Working...") # Press 'q' to exit if keyboard.is_pressed('q'): print("Loop terminated by user.") break if __name__ == "__main__": main_loop() Output:Working...Working...Working...Working...Loop terminated by user.Method 3: Using msvcrt Module (Windows Only)For Windows users, this method captures key presses without extra libraries. Python import msvcrt def main_loop(): while True: print("Working...") # Check if any key is pressed if msvcrt.kbhit(): key = msvcrt.getch().decode() if key == 'q': print("Loop terminated by user.") break if __name__ == "__main__": main_loop() Output:Working...Working...Working...Working...Loop terminated by user.Explanation:msvcrt.kbhit() checks for any key press.msvcrt.getch() captures the pressed key (e.g., 'q') to break the loop.Also read: KeyboardInterrupt, keyboard library. Comment More infoAdvertise with us Next Article How to Kill a While Loop with a Keystroke in Python? gopikumarkaushik9065 Follow Improve Article Tags : Python Python Programs python-basics Python-exceptions Practice Tags : python Similar Reads How to Emulate a Do-while loop in Python? We have given a list of strings and we need to emulate the list of strings using the Do-while loop and print the result. In this article, we will take a list of strings and then emulate it using a Do-while loop. Do while loop is a type of control looping statement that can run any statement until th 3 min read Loop Through a List using While Loop in Python In Python, the while loop is a versatile construct that allows you to repeatedly execute a block of code as long as a specified condition is true. When it comes to looping through a list, the while loop can be a handy alternative to the more commonly used for loop. In this article, we'll explore fou 3 min read How to skip Iterations in For Loop - Python Loops are generally used to repeat the tasks. Sometimes, we may want to skip certain steps in the loop. We can do this easily with the continue statement. This article explains how to skip iterations in a for loop.In a for loop, you can use continue to skip a specific iteration when a condition is t 2 min read How To Catch A Keyboardinterrupt in Python In Python, KeyboardInterrupt is a built-in exception that occurs when the user interrupts the execution of a program using a keyboard action, typically by pressing Ctrl+C. Handling KeyboardInterrupt is crucial, especially in scenarios where a program involves time-consuming operations or user intera 2 min read How to Keep a Python Script Output Window Open? We have the task of how to keep a Python script output window open in Python. This article will show some generally used methods of how to keep a Python script output window open in Python. Keeping a Python script output window open after execution is a common challenge, especially when running scri 2 min read Like