{"id":850,"date":"2010-10-31T16:39:46","date_gmt":"2010-10-31T16:39:46","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/"},"modified":"2010-10-31T16:39:46","modified_gmt":"2010-10-31T16:39:46","slug":"7c-loops","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/7c-loops\/","title":{"rendered":"7C: Loops"},"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><em>Lesson 7 has three parts A, B, C which can be completed in any order.<\/em><\/p>\n<p>So far we have learned basic programming commands (e.g. assigning values, printing) and, a way to control which statements are executed using the\u00a0<code>if<\/code> statement. In this lesson, we introduce <strong>loops<\/strong> (sometimes called <strong>repetition<\/strong> or <strong>iteration<\/strong>): a way to make a computer do the same thing (or similar things) over and over. For example,\u00a0spell-checking <em>every<\/em> word in a document would be done with a loop.\u00a0We will describe the two kinds of Python loops in this lesson: <code>while<\/code> loops and <code>for<\/code> loops.<\/p>\n<h2><code>while<\/code> Loops<\/h2>\n<p>A <code>while<\/code> statement repeats a section of code over and over again as long as some condition is true. Here is an example:<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=timeLeft+%3D+5%0Awhile+timeLeft+%3E+0%3A++++++%23+condition%0A++print%28timeLeft%29++++++++%23+body%0A++timeLeft+%3D+timeLeft-1++%23+body%0Aprint%28%27Blastoff%21%27%29+++++++%23+after+loop&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=450&rightStdout=1'><\/iframe><\/p>\n<p>Here is the general structure:<\/p>\n<ul>\n<li>The first line is <code>while \u00abcondition\u00bb:<\/code> where \u00abcondition\u00bb\u00a0is an expression which returns <code>True<\/code> or <code>False<\/code>\u00a0(a Boolean expression, like with <code>if<\/code> statements).<\/li>\n<li>Afterwards, we put an indented block (again, like an <code>if<\/code> statement) consisting of the statements which we want to be repeated over and over. This is called the <em>body<\/em>.<\/li>\n<li>When you run the program, the following is repeated:\n<ul>\n<li>The condition is tested; if the condition is <code>True<\/code> then the body is executed and afterwards we repeat from the top; once the condition is evaluated to\u00a0<code>False<\/code>, the loop stops.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>So in the example above, we keep repeating the loop body until <code>timeLeft<\/code> is not greater than 0.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox0'>\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'>Countup<\/span><\/div>Modify the example above to give a program where we count <strong>up <\/strong>starting from 1 and going up until <strong>10<\/strong>, and then print <code>Blastoff!<\/code><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='usercode0' id='usercode0'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput0\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch0\" value=\"Input Switch\" onclick=\"pbInputSwitch(0,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(0)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(0,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect0' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(0,'7c.countup')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(0);\" >Help<\/option>\n<\/select><\/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=\"47ecfba3447f586a963a2b52e6a4e207\"\/>\n<div id='pbresults0' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(0);});pbInputSwitch(0,\"N\");<\/script>\n<\/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> With loops, it is easy to write a program that runs forever in an <em>infinite loop<\/em>. <form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox1'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Infinite loop<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nwhile True:\n x=1<\/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<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(1)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(1,'N')\" ><\/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=\"7a48ca21c722ec9b36719434e8bb9f57\"\/>\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 You get the error \"Time Limit Exceeded\" because the CS\u00a0Circles web server enforces a time limit; after 1 second the program is terminated. If you <a href=\"\/run-at-home\/\">ran the program at home<\/a>, it would run forever (until you yourself force it to terminate, usually by pressing Ctrl-C).\u00a0 <\/span><\/td><\/table><\/p>\n<h2><code>for<\/code> Loops<\/h2>\n<p>There is another kind of loop in Python called a <code>for<\/code> loop. In many situations either kind of loop (<code>for<\/code>\/<code>while<\/code>) can be used but one is simpler than another, so it is useful to know how to use both.\u00a0A <code>for<\/code> loop is built in order to easily loop through a range of numbers (or as we will see in a later lesson, any list of data).<\/p>\n<p>Here is an example of a <code>for<\/code> loop.<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=for+counter+in+range%2810%2C+15%29%3A%0A++print%28%22counter+is%22%2C+counter%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<p>The general structure of a numerical <code>for<\/code> loop is<\/p>\n<pre>for \u00abvariableName\u00bb in range(\u00abstartValue\u00bb, \u00abtailValue\u00bb):\n  \u00abindented block of commands, called the loop \"body\"\u00bb<\/pre>As usual, the body block can be multiple lines long, as long as all of those lines are indented by the same amount. First the loop body is executed with\u00a0<code>variableName<\/code> set to <code>startValue.<\/code> Then it repeats with\u00a0<code>variableName<\/code> set to <code>startValue+1<\/code>, then again with <code>startValue+2<\/code>, et cetera.\u00a0This continues until\u00a0<code>variableName<\/code> has value <code>tailValue-1<\/code>, and afterwards the loop stops.<\/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> The loop ends with <code>tailValue-1<\/code>, and not <code>tailValue<\/code>!<form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox2'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nfor i in range(1, 3):\n print(i)             # 3 is not printed!<\/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<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'N')\" ><\/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=\"ec062eba34471710d281441d0e0b4b82\"\/>\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 <\/span><\/td><\/table><\/p>\n<p>Here is an example of a <code>for<\/code> loop inside another <code>for<\/code> loop.<\/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=\"title\">Example<\/span><\/div>This code prints a 5\u00d75\u00a0square of ones. \u00a0<br\/><strong>Note<\/strong>: when we multiply a number\u00a0<code>X<\/code>\u00a0by ten and add one, we're essentially putting an extra\u00a0<code>1<\/code>\u00a0digit at the end of\u00a0<code>X<\/code>. For example, (1867*10)+1=18671.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nfor i in range(0, 5):\n  X = 0\n  for j in range(0, 5):\n    X = (X*10)+1\n  print(X)<\/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<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(3)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(3,'N')\" ><\/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=\"c22447252b471c197803c7c39c6ccadb\"\/>\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><form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox4'>\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'>One Triangle<\/span><\/div>Modify the previous program in two ways. First, instead of a square, make it draw a triangle shaped like this: \u25e4. Second, instead of always having 5 lines, it should take the desired size as input from <code>input()<\/code>. For example, if the input is <code>3<\/code>, then the output should be <\/p>\n<pre>111<br\/>11<br\/>1<\/pre> <a class=\"hintlink\"  id=\"hintlink5\">Click here for a hint.<\/a>\u00a0<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='usercode4' id='usercode4'  cols=10 rows=20   class='pyboxCode RW'>\nn=int(input())\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput4\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode4' value='n=int(input())\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch4\" value=\"Input Switch\" onclick=\"pbInputSwitch(4,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(4)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(4,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect4' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(4,'7c.triangle')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(4,descape($('#defaultCode4').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(4);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse4\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"4\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"6876e6ab561ddb4bb4175cf01346e381\"\/>\n<div id='pbresults4' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(4);});pbInputSwitch(4,\"N\");<\/script>\n<\/p>\n<h2>The <code>break<\/code> and <code>continue<\/code> Statements<\/h2>\n<p>The <code>break<\/code> statement is like an emergency escape for a <code>while<\/code> or <code>for<\/code> loop: <code>break<\/code> causes an immediate jump to the commands after the end of the loop body. Here is an example using <code>break<\/code>: it reads all lines of input until it finds one that says\u00a0<code>\"END\"<\/code>.<\/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=\"title\">Example<\/span><\/div>Looping through all lines of input<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 188px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=7 readonly='readonly'  style = 'height : 188px;'  class='pyboxCode RO'>\ncounter = 0\nwhile True:\n  lineIn = input()\n  if lineIn == 'END':\n    break\n  counter = counter+1\n  print('line', counter, '=', lineIn)<\/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<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(6)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(6,'N')\" ><\/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=\"81a9e3510f5e28d6521c90a9167dff6a\"\/>\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>The <code>continue<\/code> statement makes you <strong>skip<\/strong> the rest of a loop, then repeat the body from the next round (usually called the next \"iteration\").<\/p>\n<p><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><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nfor n in range(10, 16):  \n  if (n == 13):  # bad luck\n    continue     # so we skip it\n  print(n)<\/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<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(7)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(7,'N')\" ><\/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=\"e6fc0fd5cc46bc8694a13b7ab1650ec9\"\/>\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<\/p>\n<p>Here is a visualized example that combines <code>break<\/code> and <code>continue<\/code>. Can you predict what it will output?<\/p>\n<p><iframe width='100%' height='480' frameborder='0' scrolling='no' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/OnlinePythonTutor3-cemc\/iframe-embed.html#code=for+i+in+range%281%2C+16%29%3A%0A++if+%28i+%3D%3D+4%29%3A%0A++++continue%0A++if+%28i+%3D%3D+9%29%3A%0A++++break%0A++print%28i%29%0Aprint%28%27Done%27%29&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0&resizeContainer=true&highlightLines&width=400&rightStdout=1'><\/iframe><\/p>\n<h2>Exercises<\/h2>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox8'>\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'>Square Census<\/span><\/div>The square numbers are the integers of the form K \u00d7 K, e.g. 9 is a square number since 3 \u00d7 3 = 9. Write a program that reads an integer <em>n<\/em> from input and outputs all the positive square numbers less than <em>n<\/em>, one per line in increasing order. For example, if the input is 16, then the correct output would be <\/p>\n<pre>1<br\/>4<br\/>9<\/pre> <a class=\"hintlink\"  id=\"hintlink9\">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='usercode8' id='usercode8'  cols=10 rows=20   class='pyboxCode RW'>\nn=int(input())\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput8\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode8' value='n=int(input())\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch8\" value=\"Input Switch\" onclick=\"pbInputSwitch(8,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(8)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(8,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect8' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(8,'7c.squares')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(8,descape($('#defaultCode8').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(8);\" >Help<\/option>\n<\/select><\/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=\"2d531794dfbb131558c4d9158e699e01\"\/>\n<div id='pbresults8' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(8);});pbInputSwitch(8,\"N\");<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform10\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox10'>\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'>Skipping<\/span><\/div>Extend the \"Looping through all lines of input\" example above (we've copied it for you) by adding a new feature: any line equal to <code>SKIP<\/code> should not be printed, and should not cause the counter to be increased. Run the program to see an example. <a class=\"hintlink\"  id=\"hintlink11\">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='usercode10' id='usercode10'  cols=10 rows=20   class='pyboxCode RW'>\ncounter = 0\nwhile True:\n  lineIn = input()\n  if lineIn=='END':\n    break\n  counter = counter+1\n  print('line', counter, '=', lineIn)\n<\/textarea><\/div>\n<div id='pbhistory10' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput10\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode10' value='counter = 0\\nwhile True:\\n  lineIn = input()\\n  if lineIn==\\u0027END\\u0027:\\n    break\\n  counter = counter+1\\n  print(\\u0027line\\u0027, counter, \\u0027=\\u0027, lineIn)\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit10' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch10\" value=\"Input Switch\" onclick=\"pbInputSwitch(10,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(10)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(10,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect10' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(10,'7c.skip')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(10,descape($('#defaultCode10').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(10);\" >Help<\/option>\n<\/select><\/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=\"72e252debdf7bde5e95b8768ecbf53ed\"\/>\n<div id='pbresults10' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(10);});pbInputSwitch(10,\"N\");<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform12\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox12'>\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'>Finding Factors<\/span><\/div>If <em>a<\/em> \u00d7 <em>b<\/em> = <em>n<\/em>, we call <em>a<\/em> \u00d7 <em>b<\/em> a <strong>factorization<\/strong>\u00a0of <em>n<\/em>. In this exercise, write a program that takes a positive integer <em>n<\/em> from input, and then outputs all factorizations of <em>n<\/em>; you should follow the formatting given by the following example for <em>n<\/em>=10. <\/p>\n<pre>1 times 10 equals 10<br\/>2 times 5 equals 10<br\/>5 times 2 equals 10<br\/>10 times 1 equals 10<\/pre> <a class=\"hintlink\"  id=\"hintlink13\">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='usercode12' id='usercode12'  cols=10 rows=20   class='pyboxCode RW'>\nn = int(input())\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory12' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput12\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<input type='hidden' id='defaultCode12' value='n = int(input())\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit12' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch12\" value=\"Input Switch\" onclick=\"pbInputSwitch(12,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(12)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(12,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect12' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(12,'7c.factorize')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(12,descape($('#defaultCode12').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(12);\" >Help<\/option>\n<\/select><\/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=\"9182120ebc80fb0bc457f3936f5d3549\"\/>\n<div id='pbresults12' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(12);});pbInputSwitch(12,\"N\");<\/script>\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lesson 7 has three parts A, B, C which can be completed in any order. So far we have learned basic programming commands (e.g. assigning values, printing) and, a way to control which statements are executed using the\u00a0if statement. In &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/7c-loops\/\">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-850","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/850"}],"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=850"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/850\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}