{"id":1535,"date":"2011-02-05T13:31:00","date_gmt":"2011-02-05T13:31:00","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/"},"modified":"2011-02-05T13:31:00","modified_gmt":"2011-02-05T13:31:00","slug":"11a-lower-case","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/11a-lower-case\/","title":{"rendered":"11A: Lower Case"},"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 11 has three parts A, B, C which can be completed in any order.<\/em><\/p>\n<p>This lesson contains an exercise where you need to write two functions: one will use the other to accomplish its goal. The goal is to eventually write a function <code>lowerString<\/code> that can convert all of the letters in a string to <em>lower case<\/em>. (A, B, C are <em>upper case<\/em> letters and a, b, c are <em>lower case<\/em>.) For example, the result of<\/p>\n<pre>lowerString(\"This string has 9 CAPITAL letters (&amp; Punctuation)!\")<\/pre>should be<\/p>\n<pre>\"this string has 9 capital letters (&amp; punctuation)!\"<\/pre>\n<h2>Step 1: Characters<\/h2>\n<p>The first step is to write a function <code>lowerChar(char)<\/code> that can return the result of converting a single character <code>char<\/code> to lower case. It should do the following:<\/p>\n<ul>\n<li>if the input character <code>char<\/code> is a capital letter (between '<code>A<\/code>' and '<code>Z<\/code>'), it should return the lower-case version of the letter (between '<code>a<\/code>' and '<code>z<\/code>')<\/li>\n<li>in all other cases, it should return the same\u00a0<code>char<\/code> which was input.<\/li>\n<\/ul>\n<p>(In order to do the first step, you will have to use an <a href=\"\/6-if\/\"><code>if<\/code> statement<\/a>, an <a href=\"\/9-else-and-or-not\/\"><code>and<\/code> operator<\/a>, and apply some knowledge from <a href=\"\/7a-strings\/\">the lesson about strings<\/a>.)<\/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'>Lower-case Characters<\/span><\/div>Define a function <code>lowerChar(char)<\/code> which meets the above description.<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\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> 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,'Y')\" ><\/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,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect0' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(0,'11a.lowerchar')\" >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=\"8f4ff707d6a4dd6764c2d2f6dd47d767\"\/>\n<div id='pbresults0' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(0);});pbInputSwitch(0,\"Y\");<\/script>\n<\/p>\n<h2>Step 2: Strings<\/h2>\n<p>Now, you will write a second function <code>lowerString(string)<\/code> which will return the result of converting the entire string to lower case, by calling <code>lowerChar<\/code> on each character. We suggest you do this as follows:<\/p>\n<ul>\n<li>first, copy the definition of <code>lowerChar(char)<\/code> from your solution to the first part<\/li>\n<li>then define a second function, <code>lowerString(string)<\/code>\n<ul>\n<li>on the first line inside lowerString, initialize a variable <code>result = \"\"<\/code> equal to the empty string<\/li>\n<li>use a <a href=\"\/7c-loops\/\">for loop<\/a> with\u00a0<code>i<\/code>\u00a0and set <code>result = result + lowerChar(string[i])<\/code><\/li>\n<li>finally, <code>return result<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox1'>\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'>Lower-case Strings<\/span><\/div>Define a function\u00a0<code>lowerString(string)<\/code> which returns the result of converting <code>string<\/code> to lower case.<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='usercode1' id='usercode1'  cols=10 rows=20   class='pyboxCode RW'>\n# first, copy your definition of lowerChar() here\n# then define lowerString(string)\n<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput1\">Enter testing statements like <code>print(myfunction(\"test argument\"))<\/code> 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='defaultCode1' value='# first, copy your definition of lowerChar() here\\n# then define lowerString(string)\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch1\" value=\"Input Switch\" onclick=\"pbInputSwitch(1,'Y')\" ><\/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,'Y')\" ><\/td>\n<\/tr><\/table><select id='pbSelect1' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(1,'11a.lowerstring')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(1,descape($('#defaultCode1').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(1);\" >Help<\/option>\n<\/select><\/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=\"21f7e22a37bec66b3333ff8dd15b1fc6\"\/>\n<div id='pbresults1' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(1);});pbInputSwitch(1,\"Y\");<\/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> Later on, you will learn about the <code>string.lower()<\/code> method, which is a built-in way to perform this task. <\/span><\/td><\/table><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lesson 11 has three parts A, B, C which can be completed in any order. This lesson contains an exercise where you need to write two functions: one will use the other to accomplish its goal. The goal is to &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/11a-lower-case\/\">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-1535","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/1535"}],"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=1535"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/1535\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=1535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}