Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Keyboard Errors

#1
Python Keyboard Errors

<div><p>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.</p>
<h2>What is “Keyboard Interrupt”?</h2>
<p>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 <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>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Energizer Bunny® - 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>
</div>
</figure>
<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>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...300x84.png 300w, https://blog.finxter.com/wp-content/uplo...150x42.png 150w" sizes="(max-width: 364px) 100vw, 364px" /></figure>
</div>
<p>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 <strong><em>Keyboard Interrupt</em></strong>.</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...300x74.png 300w, https://blog.finxter.com/wp-content/uplo...150x37.png 150w" sizes="(max-width: 355px) 100vw, 355px" /></figure>
</div>
<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’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 <a href="https://docs.python.org/3/library/exceptions.html#exception-hierarchy" target="_blank" rel="noreferrer noopener">here</a>.</p>
<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’s to stop the script from running.</p>
<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>
<h2>The Try/Except method</h2>
<p>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 <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>
<p>This is the simplest example that we can provide for how it works.</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...300x61.png 300w, https://blog.finxter.com/wp-content/uplo...150x30.png 150w" sizes="(max-width: 578px) 100vw, 578px" /></figure>
</div>
<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’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.</p>
<p>You might not think you’ll need it, but once you start catching errors when running your code, you’ll want to use it.</p>
<p>To get it to work, we’re going to make a few adjustments.</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...00x114.png 300w, https://blog.finxter.com/wp-content/uplo...150x57.png 150w" sizes="(max-width: 546px) 100vw, 546px" /></figure>
</div>
<p>At the top of our script, we imported the Sys module, which is built-in for Python. You don’t need to install it.</p>
<p>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 <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>
<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’t go crazy with your print statements.  Put them all underneath your <code>try</code> statement.</p>
<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’s 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>
<p>In fact, you don’t 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>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...00x284.png 300w, https://blog.finxter.com/wp-content/uplo...50x142.png 150w" sizes="(max-width: 463px) 100vw, 463px" /></figure>
</div>
<p>Works pretty good, don’t you think?</p>
<h2>Let’s Build A Time Loop</h2>
<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’s build it differently and see how that can work. And we’ll set it up so you can’t escape from the loop.</p>
<p>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.</p>
<p>First, you’ll 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’ll be making some sleep functions later on in our code. But first, we’re going to create the time loop function.</p>
<p>It will be simple. Just one print statement involved. However, you can create as many print statements as your heart desires.</p>
<p>The function will look like this:</p>
<figure class="wp-block-image size-large"><img 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/uplo...300x41.png 300w, https://blog.finxter.com/wp-content/uplo...150x20.png 150w" sizes="(max-width: 515px) 100vw, 515px" /></figure>
<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’ll include the <code>time_loop()</code> 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!</p>
<p>While under the <code>except</code> 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 <strong><em>Palm Springs</em></strong> recently and that fills up about 98% of our knowledge on the topic.</p>
<p>But your time loop should end up looking like this:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...00x145.png 300w, https://blog.finxter.com/wp-content/uplo...150x73.png 150w" sizes="(max-width: 529px) 100vw, 529px" /></figure>
</div>
<p>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? <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f60a.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<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>
<p> <iframe 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>
</div>


https://www.sickgaming.net/blog/2020/09/...rd-errors/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016