{"id":4383,"date":"2012-05-28T16:33:13","date_gmt":"2012-05-28T20:33:13","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=4383"},"modified":"2019-08-27T17:04:36","modified_gmt":"2019-08-27T09:04:36","slug":"1e-errors","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/1e-errors\/","title":{"rendered":"1E: Errors"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p>As you do more and more programming, you will naturally encounter a lot of errors (or <em>bugs<\/em>). Causing, understanding, and fixing errors is an important part of programming. Python will do its best to run anything that you tell it to run, but if it can't understand what you're asking, then it won't run the program. All the same, Python will try to tell you a little bit of information about what went wrong, in order to help you try to fix it.<\/p>\n<p>Here are two Python errors.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox0'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>A Syntax Error<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nprint \"Gee golly\"<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"1f60d53abbdb5b1f708b6999e4ea05f2\"\/>\n<div id='pbresults0' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit0\").value = \"Run program\";document.getElementById(\"inputInUse0\").value = \"N\";<\/script>\n<\/p>\n<p>In this first example, we forget to use the parenthesis that are required by <code>print(...)<\/code>. Python does not understand what you are trying to do.<\/p>\n<p>Here is a second example of a bug in Python.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox1'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>A Run-Time Error<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nprint(greeting)<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse1\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"1\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"d537913a9837de0249e9af12b7818e6c\"\/>\n<div id='pbresults1' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n<\/p>\n<p>In the second example, we forget to define the <code>greeting<\/code> variable. Python knows what you want it to do, but since no <code>greeting<\/code> has been defined, an error occurs.<\/p>\n<p>A <strong>syntax error<\/strong> happens when Python can't understand what you are saying. A <strong>run-time error<\/strong> happens when Python understands what you are saying, but runs into trouble when following your instructions.<\/p>\n<p>In English, a <strong>syntax error<\/strong> would be like the sentence<\/p>\n<p style=\"text-align: center;\"><em>Please cat dog monkey.<\/em><\/p>\n<p>The grammar of this sentence does not make sense. From the perspective of English grammar, it is missing a <em>verb <\/em>(action). You cannot understand what you are being asked to do. <em>Syntax<\/em> means the same thing as <em>grammar<\/em>.<\/p>\n<p>In English, a <strong>run-time error<\/strong> would be like the sentence<\/p>\n<p style=\"text-align: center;\"><em>Please eat the piano.<\/em><\/p>\n<p>The sentence makes sense from a grammatical perspective \u2014 there is a verb and noun in the right place \u2014 so you know what you are being asked to do. But, you will encounter problems once you start trying to eat the piano (except maybe if you are a termite). This is called a <em>run-time error<\/em> because it occurs after the program starts running.<\/p>\n<p>We also will talk about <strong>logic errors<\/strong> at the end, which means that your program runs without crashing, but still produces the wrong result. An example would be<\/p>\n<p style=\"text-align: center;\"><em>Please close the back door so that the bugs don't come in.<\/em><\/p>\n<p>This would be a logic error if the <em>front door<\/em> is also open: even though the command makes sense and you can close the back door, this doesn't actually accomplish the goal of keeping the bugs out since they'll still come in the front door.<\/p>\n<h2>Common Syntax Errors in Python<\/h2>\n<p>Here are a few additional examples of syntax errors that can occur in Python. One very general thing that can occur is that Python will encounter a special symbol in a place that it doesn't expect.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox2'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>Syntax Error<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nprint(Hello, World!)<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"80d6dfb117099dfcb872a4f163891626\"\/>\n<div id='pbresults2' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<p>Python says <code>SyntaxError: invalid syntax<\/code> and points with <code>^<\/code> to the exclamation point. The problem is that <code>!<\/code> has no meaning in Python. The syntax error would go away if we had put <code>print(\"Hello, World!\")<\/code> instead, because then Python would understand that the <code>!<\/code> is part of the text with <code>Hello, World<\/code>.<\/p>\n<p>Here is another syntax error that is more subtle.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox3'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>Syntax Error<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nclass = \"Advanced Computronics for Beginners\"<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"118855f6921ffbe7b56deadcc1d42a35\"\/>\n<div id='pbresults3' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit3\").value = \"Run program\";document.getElementById(\"inputInUse3\").value = \"N\";<\/script>\n<\/p>\n<p>The problem is that <code>class<\/code> is a special word in Python. if you had written <code>course<\/code> instead of class it would have been fine. Click <a class=\"hintlink\"  id=\"hintlink4\">here<\/a> to see the list of all special \"keywords\" in Python.<\/p>\n<p>If you are using quotes around text and you forget the second one, or you are using parentheses and forget the second one, you will get syntax errors:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox5'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>Syntax Error<\/span><\/div>Forgetting the second quote<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode5' id='usercode5'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nname = \"Jim<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"2b9b36f18baf10a204853abe445376e8\"\/>\n<div id='pbresults5' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/p>\n<p>In this error, <strong>EOL<\/strong> is short for <strong>E<\/strong>nd <strong>O<\/strong>f <strong>L<\/strong>ine: Python expected another <code>\"<\/code> but the line ended before it was found.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox6'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>Syntax Error<\/span><\/div>Forgetting the second parenthesis<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nprint(77<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"baaaabc8327952d3de7cddd98e61eb2f\"\/>\n<div id='pbresults6' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\n<\/p>\n<p>Similarly, <strong>EOF<\/strong> is short for <strong>E<\/strong>nd <strong>O<\/strong>f <strong>F<\/strong>ile: Python kept looking for a <code>)<\/code> but the program file ended before it was found.<\/p>\n<p>Sometimes two very similar syntax errors can give two very different error messages. But, every error message is indeed trying to tell you something helpful.<\/p>\n<h2>Run-Time Errors<\/h2>\n<p>Here are a few common run-time errors. Python is able to understand what the program says, but runs into problems when actually performing the instructions.<\/p>\n<ul>\n<li>using an undefined variable or function. This can also occur if you use capital letters inconsistently in a variable name: <form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox7'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>An undefined variable<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\ncallMe = \"Maybe\"\nprint(callme)<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse7\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"7\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"4f31cdc433eda08b09f9520f4297481d\"\/>\n<div id='pbresults7' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit7\").value = \"Run program\";document.getElementById(\"inputInUse7\").value = \"N\";<\/script>\n<\/li>\n<li>dividing by zero, which makes no sense in mathematics. (Why? Since 0 times any number is 0, there is no solution to 1 = <em>X<\/em> * 0, so 1\/0 is undefined.) <form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox8'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Dividing by zero<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode8' id='usercode8'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nprint(1\/0)<\/textarea><\/div>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse8\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"8\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"09f9746176a2d4230f0c6fff91c10e14\"\/>\n<div id='pbresults8' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit8\").value = \"Run program\";document.getElementById(\"inputInUse8\").value = \"N\";<\/script>\n<\/li>\n<li>using operators on the wrong type of data <form class=\"pbform\" action=\"#\" id=\"pbform9\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox9'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Adding text and a number<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nprint(\"you cannot add text and numbers\" + 12)<\/textarea><\/div>\n<div id='pbhistory9' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit9' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse9\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"9\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"751a7ab390bfdd3c27346bc7449b2b2f\"\/>\n<div id='pbresults9' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit9\").value = \"Run program\";document.getElementById(\"inputInUse9\").value = \"N\";<\/script>\n<\/li>\n<\/ul>\n<p>You will find more ways of producing errors as you learn more about Python.<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> What is the technical difference between syntax and run-time errors? Here is an example comparing a run-time error to a syntax error. Look at the output of each program.<form class=\"pbform\" action=\"#\" id=\"pbform10\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox10'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>Run-Time Error<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode10' id='usercode10'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nprint(\"Here is some text\")\nprint(1\/0)<\/textarea><\/div>\n<div id='pbhistory10' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit10' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse10\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"10\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"bc12716281bbeb154fe607750970cff1\"\/>\n<div id='pbresults10' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit10\").value = \"Run program\";document.getElementById(\"inputInUse10\").value = \"N\";<\/script>\n <form class=\"pbform\" action=\"#\" id=\"pbform11\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox11'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>Syntax Error<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode11' id='usercode11'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nprint(\"Here is some text\")\nprint(1*0<\/textarea><\/div>\n<div id='pbhistory11' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit11' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse11\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"11\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"c31d93a3646a6bb4156e3a70091aaf3b\"\/>\n<div id='pbresults11' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit11\").value = \"Run program\";document.getElementById(\"inputInUse11\").value = \"N\";<\/script>\nThe program with the run-time error created some output, but the one with the syntax error did not. This is because Python runs in two steps: <\/p>\n<ol>  \t<\/p>\n<li>Python checks if your program has correct syntax, in order to determine its structure and parts.<\/li>\n<p>  \t<\/p>\n<li>If no syntax errors were encountered in step 1, then the program is executed.<\/li>\n<p> <\/ol>\n<p> <\/p>\n<div>So, a program with a syntax error will execute no steps at all, but a program with a run-time error will execute the steps that happened before the error occured.<\/div>\n<p>  <\/span><\/td><\/table><\/p>\n<h2>Logic Errors<\/h2>\n<p>Your program might run without crashing (no syntax or run-time errors), but still do the wrong thing. For example, perhaps you want a program to calculate the <em>average<\/em> of two numbers: the average of <em>x<\/em> and <em>y<\/em> is defined as<\/p>\n<p style=\"text-align: center;\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7B%5Cfrac%7Bx%2By%7D%7B2%7D%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\displaystyle{\\frac{x+y}{2}}' title='\\displaystyle{\\frac{x+y}{2}}' class='latex' \/><\/p>\n<p>Why doesn't this program work?<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform12\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox12'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>This does <em>not <\/em>calculate the average correctly.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode12' id='usercode12'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nx = 3\ny = 4\naverage = x + y \/ 2\nprint(average)<\/textarea><\/div>\n<div id='pbhistory12' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit12' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse12\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"12\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"2495b74069089ef3bf1c45449394ab3d\"\/>\n<div id='pbresults12' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit12\").value = \"Run program\";document.getElementById(\"inputInUse12\").value = \"N\";<\/script>\n<\/p>\n<p>The average should be<\/p>\n<p style=\"text-align: center;\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7B%5Cfrac%7Bx%2By%7D%7B2%7D%3D%5Cfrac%7B3%2B4%7D%7B2%7D%3D%5Cfrac%7B7%7D%7B2%7D%3D3.5%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\displaystyle{\\frac{x+y}{2}=\\frac{3+4}{2}=\\frac{7}{2}=3.5}' title='\\displaystyle{\\frac{x+y}{2}=\\frac{3+4}{2}=\\frac{7}{2}=3.5}' class='latex' \/><\/p>\n<p>but the program prints <code>5.0<\/code> instead! The error this time has to do with the \"order of operations\" in arithmetic. When you write <code>x + y \/ 2<\/code>, this has the same mathematical meaning as <img src='https:\/\/s0.wp.com\/latex.php?latex=x+%2B+%28y+%2F+2%29+%3D+3+%2B+%284+%2F+2%29+%3D+3+%2B+2+%3D+5&#038;bg=T&#038;fg=000000&#038;s=2' alt='x + (y \/ 2) = 3 + (4 \/ 2) = 3 + 2 = 5' title='x + (y \/ 2) = 3 + (4 \/ 2) = 3 + 2 = 5' class='latex' \/>. To fix the problem, the third line of our program should be written as <code>average = (x + y) \/ 2<\/code>, which makes clear to Python that we really want the value <img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cfrac%7Bx%2By%7D%7B2%7D&#038;bg=T&#038;fg=000000&#038;s=3' alt='\\frac{x+y}{2}' title='\\frac{x+y}{2}' class='latex' \/>, where we add first and divide afterwards.<\/p>\n<p>You can have logic errors because you <em>designed<\/em> a program incorrectly, or because you didn't write code that follows the design correctly (like the <code>average<\/code> example). Logic errors can be difficult to spot, especially in a longer program, but as you get better at writing code you will also get better at avoiding logic errors. Lesson 6D will give some tips on avoiding logic errors.<\/p>\n<h2>Exercises<\/h2>\n<p>Now that the lesson is complete, we have three exercises on <em>debugging<\/em> (fixing errors in programs). You can try to spot the errors before running the programs, or you can run them first and use Python's response to determine what needs fixing.<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Each program comes with some code already set up for you. You only need to change a few characters (letters\/symbols\/numbers) to fix each program. <em>The grader will reject any solution that changes too many characters.<\/em> Select <strong>Reset code to default<\/strong> if you want to bring back the original version. <\/span><\/td><\/table><\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform13\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox13'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Summer<\/span><\/div> Fix the syntax error in the following program, so that it prints out the sum of all the numbers from 1 to 10. You can change at most one character. <div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode13' id='usercode13'  cols=10 rows=20   class='pyboxCode RW'>\nprint(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 +)\n<\/textarea><\/div>\n<div id='pbhistory13' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode13' value='print(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 +)\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit13' value=' '\/><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(13,'1e.product')\" ><\/td>\n<td><input type='button' name='default' value=\"Reset code to default\" onclick=\"pbSetText(13,descape($('#defaultCode13').val()))\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(13);\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse13\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"13\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"999cf565b634dfbfd8a469118354436e\"\/>\n<div id='pbresults13' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit13\").value = \"Run program\";document.getElementById(\"inputInUse13\").value = \"N\";<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform14\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox14'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Hello Joe<\/span><\/div> Fix the run-time error in the following program, so that it prints out <code>Hello<\/code> on the first line and <code>Joe<\/code> on the second line. You can change at most two characters. <div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode14' id='usercode14'  cols=10 rows=20   class='pyboxCode RW'>\nprint(\"Hello\")\nusername = Joe\nprint(username)\n<\/textarea><\/div>\n<div id='pbhistory14' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode14' value='print(\\\"Hello\\\")\\nusername = Joe\\nprint(username)\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit14' value=' '\/><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(14,'1e.joe')\" ><\/td>\n<td><input type='button' name='default' value=\"Reset code to default\" onclick=\"pbSetText(14,descape($('#defaultCode14').val()))\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(14);\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse14\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"14\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"30580aca03b74f0674c6346972510a35\"\/>\n<div id='pbresults14' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit14\").value = \"Run program\";document.getElementById(\"inputInUse14\").value = \"N\";<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform15\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox15'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Shopping<\/span><\/div> You are going shopping for meat and milk, but there is tax. You buy $2.00 of milk and $4.00 of meat, and the tax rate is 3%. Print out the total cost of your groceries (you don't need to print the dollar sign). <a class=\"hintlink\"  id=\"hintlink16\">Hint<\/a> <div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode15' id='usercode15'  cols=10 rows=20   class='pyboxCode RW'>\nmeatPrice = 4.00\nmeatTax = 0.03 * meatPrice\nmilkPrice = 2.00\nmilkTax = 0.03 * milkPrice\nprint(meatTax + meatPrice + milkTax + meatPrice)\n<\/textarea><\/div>\n<div id='pbhistory15' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode15' value='meatPrice = 4.00\\nmeatTax = 0.03 * meatPrice\\nmilkPrice = 2.00\\nmilkTax = 0.03 * milkPrice\\nprint(meatTax + meatPrice + milkTax + meatPrice)\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit15' value=' '\/><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(15,'1e.shopping')\" ><\/td>\n<td><input type='button' name='default' value=\"Reset code to default\" onclick=\"pbSetText(15,descape($('#defaultCode15').val()))\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(15);\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse15\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"15\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"eb590239154dc1264179f994ee0f0fa2\"\/>\n<div id='pbresults15' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit15\").value = \"Run program\";document.getElementById(\"inputInUse15\").value = \"N\";<\/script>\n<\/p>\n<p>Once you have squashed all of the bugs, move on to the next lesson!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you do more and more programming, you will naturally encounter a lot of errors (or bugs). Causing, understanding, and fixing errors is an important part of programming. Python will do its best to run anything that you tell it &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/1e-errors\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4383","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/4383"}],"collection":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/comments?post=4383"}],"version-history":[{"count":5,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/4383\/revisions"}],"predecessor-version":[{"id":11463,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/4383\/revisions\/11463"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=4383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}