Python Keyboard Errors

Rate this post

When you’re learning and working with Python, you’re going to encounter errors and there’s no getting around that fact. So how do you get around them? It helps to learn about the errors. We’re going to take the opportunity to learn one of them today.

What is “Keyboard Interrupt”?

This is an error that you’re not likely to run across often unless you are running a Python program for more than two seconds. If your program has loops, a while loop specifically, then there might come a point when you need to stop the program. Because while loops, when not written with an ending in mind, keep going, like the Energizer Bunny.

Energizer Bunny® - Darth Vader - 1994 Commercial

In this example, we created a simple while loop that is loaded with print statements. If you run it, then the script will keep printing the same three statements.

Impressive, isn’t it? You can keep it going for a long time, but you might want to think about stopping it. When you press Ctrl and C, you’ll get this Exception that pops up, otherwise known as the Keyboard Interrupt.

This is one of the built-in exceptions that you’ll come across when programming in Python. Based on the hierarchy, the Keyboard Interruption exception is right towards the top, underneath Base Exception and System Exit. You can find the full hierarchy here.

Now you might be wondering if there is any way you can keep that error from popping up? Not really. It is there for a reason and it’s to stop the script from running.

Unless you want to keep it going forever. We hope your computer is built for that. However, we do know of a good way for you to clean it up a little bit.

The Try/Except method

If you’re hoping to avoid those awkward error messages that pop up when running your Python code, then this is the best route for you to go. The Try/Except method is another solid way for you to run your Python code. And you can do so without dealing with specific errors in your code

This is the simplest example that we can provide for how it works.

You put the code that you want to run underneath the try. And underneath your except is what you can enter to deal with any errors you might encounter. This can help you with larger projects. If you’re building a Twitter bot, for example, you could set it up so that it runs your code and if there’s a problem with getting the tweet out, you’ll be able to catch the error.

You might not think you’ll need it, but once you start catching errors when running your code, you’ll want to use it.

To get it to work, we’re going to make a few adjustments.

At the top of our script, we imported the Sys module, which is built-in for Python. You don’t need to install it.

Inside of our while loop, we enter our Try and Except block. Underneath Try, we put in three print statements. You’re free to put as many print statements as you want in this. If you want to make it 10, then go for it! We want you to be ambitious with your infinite time loop.

Underneath except, we just have one print statement in there. Obviously, you can do more, but that would defeat the purpose. Please don’t go crazy with your print statements.  Put them all underneath your try statement.

What we put in next, underneath our print statement, is what you would consider an exit command. And there’s more than one you can use. However, for this instance, we just chose sys.exit(). You can also import os and use the exit command for that one.

In fact, you don’t need to import any Python modules. You can just use quit() and it works just as well. But we like to be fancy sometimes.

Works pretty good, don’t you think?

Let’s Build A Time Loop

What we have now makes for a pretty good time loop. But now we can try to have a little more fun with our Python script. Let’s build it differently and see how that can work. And we’ll set it up so you can’t escape from the loop.

Now when we say you won’t escape, we mostly mean it won’t be as simple as pressing Ctrl + C on your keyboard. If you’re worried about stopping it, all you would theoretically need to do is just exit out of your command line. It would stop at that point. Of course, you’d have to start all over by re-opening your line. But let’s have some fun.

First, you’ll need to import the Time module, which is built-in already for Python. We’ll be making some sleep functions later on in our code. But first, we’re going to create the time loop function.

It will be simple. Just one print statement involved. However, you can create as many print statements as your heart desires.

The function will look like this:

Once that is done, we can make our while loop. Embedded in it will be our try and except blocks. Underneath try, we’ll include the time_loop() function, as well as our sleep function. Inside the parenthesis, you’ll want to put in how long you want the program to sleep for. It’s done in seconds. You could have it at 1, 100, 1000, 10000, whatever you want. For our example, we chose five seconds. It should be a little easier on your eyes, instead of having it go non-stop. Gives you that breath of fresh air!

While under the except one, we add another print statement. It might seem cruel to you, but hey it’s a time loop. You’re stuck! Isn’t that how time loops work, at least? We don’t know for sure. We watched Palm Springs recently and that fills up about 98% of our knowledge on the topic.

But your time loop should end up looking like this:

Pretty cool, right? You’ll have a fun and frustrating time trying to escape this time loop. Of course, like we said, you can always exit out of command line. But why take the risk? ?

If nothing else, now is your chance to play around with the script. Have some fun. Maybe try to do something different with your time loop. Get a little creative!