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