{"id":117972,"date":"2020-09-14T11:10:49","date_gmt":"2020-09-14T11:10:49","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=13050"},"modified":"2020-09-14T11:10:49","modified_gmt":"2020-09-14T11:10:49","slug":"python-keyboard-errors","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/09\/14\/python-keyboard-errors\/","title":{"rendered":"Python Keyboard Errors"},"content":{"rendered":"<p>When you\u2019re learning and working with Python, you\u2019re going to encounter errors and there\u2019s no getting around that fact. So how do you get around them? It helps to learn about the errors. We\u2019re going to take the opportunity to learn one of them today.<\/p>\n<h2>What is \u201cKeyboard Interrupt\u201d?<\/h2>\n<p>This is an error that you\u2019re not likely to run across often unless you are running a Python program for more than two seconds. If your program has <a href=\"https:\/\/blog.finxter.com\/python-loops\/\" title=\"Python Loops\" target=\"_blank\" rel=\"noreferrer noopener\">loops<\/a>, a <code>while<\/code> loop specifically, then there might come a point when you need to stop the program. Because <code><a href=\"https:\/\/blog.finxter.com\/python-one-line-while-loop-a-simple-tutorial\/\" title=\"Python One Line While Loop [A Simple Tutorial]\">while<\/a><\/code> loops, when not written with an ending in mind, keep going, like the <em>Energizer Bunny<\/em>.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Energizer Bunny\u00ae - Darth Vader - 1994 Commercial\" width=\"1333\" height=\"1000\" src=\"https:\/\/www.youtube.com\/embed\/QxafIhYFOr0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>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.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-34.png\" alt=\"\" class=\"wp-image-13052\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-34.png 364w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-34-300x84.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-34-150x42.png 150w\" sizes=\"(max-width: 364px) 100vw, 364px\" \/><\/figure>\n<\/div>\n<p>Impressive, isn\u2019t 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\u2019ll get this Exception that pops up, otherwise known as the <strong><em>Keyboard Interrupt<\/em><\/strong>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-35.png\" alt=\"\" class=\"wp-image-13053\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-35.png 355w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-35-300x74.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-35-150x37.png 150w\" sizes=\"(max-width: 355px) 100vw, 355px\" \/><\/figure>\n<\/div>\n<p>This is one of the <a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html#KeyboardInterrupt\" target=\"_blank\" rel=\"noreferrer noopener\">built-in exceptions<\/a> that you\u2019ll 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 <a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html#exception-hierarchy\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n<p>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\u2019s to stop the script from running.<\/p>\n<p>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.<\/p>\n<h2>The Try\/Except method<\/h2>\n<p>If you\u2019re 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 <a href=\"https:\/\/docs.python.org\/3\/tutorial\/errors.html#handling-exceptions\">Try\/Except method<\/a> is another solid way for you to run your Python code. And you can do so without dealing with specific errors in your code<\/p>\n<p>This is the simplest example that we can provide for how it works.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-36.png\" alt=\"\" class=\"wp-image-13055\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-36.png 578w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-36-300x61.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-36-150x30.png 150w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/figure>\n<\/div>\n<p>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\u2019re building a Twitter bot, for example, you could set it up so that it runs your code and if there\u2019s a problem with getting the tweet out, you\u2019ll be able to catch the error.<\/p>\n<p>You might not think you\u2019ll need it, but once you start catching errors when running your code, you\u2019ll want to use it.<\/p>\n<p>To get it to work, we\u2019re going to make a few adjustments.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-37.png\" alt=\"\" class=\"wp-image-13056\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-37.png 546w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-37-300x114.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-37-150x57.png 150w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/figure>\n<\/div>\n<p>At the top of our script, we imported the Sys module, which is built-in for Python. You don\u2019t need to install it.<\/p>\n<p>Inside of our while loop, we enter our Try and Except block. Underneath Try, we put in three print statements. You\u2019re free to put as many <a href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Print Function [And Its SECRET Separator &amp; End Arguments]\">print <\/a>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 <a href=\"https:\/\/blog.finxter.com\/daily-python-puzzle-infinite-while-loop\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"The Infinite While Loop in Python\">infinite <\/a>time loop.<\/p>\n<p>Underneath <code>except<\/code>, we just have one <code>print<\/code> statement in there. Obviously, you can do more, but that would defeat the purpose. Please don\u2019t go crazy with your print statements. \u00a0Put them all underneath your <code>try<\/code> statement.<\/p>\n<p>What we put in next, underneath our print statement, is what you would consider <a href=\"https:\/\/www.geeksforgeeks.org\/python-exit-commands-quit-exit-sys-exit-and-os-_exit\/\">an exit command<\/a>. And there\u2019s more than one you can use. However, for this instance, we just chose <code>sys.exit()<\/code>. You can also <code>import os<\/code> and use the <code>exit<\/code> command for that one.<\/p>\n<p>In fact, you don\u2019t need to import any Python modules. You can just use <code>quit()<\/code> and it works just as well. But we like to be fancy sometimes.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-38.png\" alt=\"\" class=\"wp-image-13057\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-38.png 463w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-38-300x284.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-38-150x142.png 150w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure>\n<\/div>\n<p>Works pretty good, don\u2019t you think?<\/p>\n<h2>Let\u2019s Build A Time Loop<\/h2>\n<p>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\u2019s build it differently and see how that can work. And we\u2019ll set it up so you can\u2019t escape from the loop.<\/p>\n<p>Now when we say you won\u2019t escape, we mostly mean it won\u2019t be as simple as pressing Ctrl + C on your keyboard. If you\u2019re 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\u2019d have to start all over by re-opening your line. But let\u2019s have some fun.<\/p>\n<p>First, you\u2019ll need to import the <a href=\"https:\/\/docs.python.org\/3\/library\/time.html\">Time module<\/a>, which is built-in already for Python. We\u2019ll be making some sleep functions later on in our code. But first, we\u2019re going to create the time loop function.<\/p>\n<p>It will be simple. Just one print statement involved. However, you can create as many print statements as your heart desires.<\/p>\n<p>The function will look like this:<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-39.png\" alt=\"\" class=\"wp-image-13058\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-39.png 515w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-39-300x41.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-39-150x20.png 150w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/figure>\n<p>Once that is done, we can make our while loop. Embedded in it will be our <code>try<\/code> and <code>except<\/code> blocks. Underneath <code>try<\/code>, we\u2019ll include the <code>time_loop()<\/code> function, as well as our sleep function. Inside the parenthesis, you\u2019ll want to put in how long you want the program to sleep for. It\u2019s 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!<\/p>\n<p>While under the <code>except<\/code> one, we add another print statement. It might seem cruel to you, but hey it\u2019s a time loop. You\u2019re stuck! Isn\u2019t that how time loops work, at least? We don\u2019t know for sure. We watched <strong><em>Palm Springs<\/em><\/strong> recently and that fills up about 98% of our knowledge on the topic.<\/p>\n<p>But your time loop should end up looking like this:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-40.png\" alt=\"\" class=\"wp-image-13059\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-40.png 529w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-40-300x145.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/09\/image-40-150x73.png 150w\" sizes=\"(max-width: 529px) 100vw, 529px\" \/><\/figure>\n<\/div>\n<p>Pretty cool, right? You\u2019ll 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? <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/1f60a.png\" alt=\"\ud83d\ude0a\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>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!<\/p>\n<p> <iframe loading=\"lazy\" height=\"800px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/NumbNauticalAmoebas?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you\u2019re learning and working with Python, you\u2019re going to encounter errors and there\u2019s no getting around that fact. So how do you get around them? It helps to learn about the errors. We\u2019re going to take the opportunity to learn one of them today. What is \u201cKeyboard Interrupt\u201d? This is an error that you\u2019re [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-117972","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/117972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=117972"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/117972\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=117972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=117972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=117972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}