{"id":560,"date":"2010-10-12T09:53:27","date_gmt":"2010-10-12T09:53:27","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/"},"modified":"2010-10-12T09:53:27","modified_gmt":"2010-10-12T09:53:27","slug":"5-input","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/5-input\/","title":{"rendered":"5: Input"},"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>In the last lesson we discussed user input, but did not really explain how user input is obtained. In Python, the user types one line of input at a time. You should use the <code><code>input()<\/code><\/code> function to actually obtain the next line of input from the user. The <code>input()<\/code> function takes no arguments and always gives back a <code>str.<\/code><\/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> On this website, all input must be specified before the program runs. If you <a href=\"\/run-at-home\/\">run Python interactively at home<\/a> then <code>input()<\/code> actually pauses the program and waits until the user types a line of text. <br\/> Also, when working interactively, you can\u00a0take advantage of the fact that <code>input()<\/code> accepts an optional string input, which will be interpreted as a prompt for the user. E.g., <\/p>\n<pre>number = input(\"Enter a number between 0 and 100. \")<\/pre>  <\/span><\/td><\/table><\/p>\n<p>Here is an example of using <code>input()<\/code> to get input. The grader will automatically specify the input for the program.<\/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=\"title\">Example<\/span><\/div>Echoing a line of text<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nline = input()\nprint(\"The first line of input is:\", line)<\/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<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><\/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=\"782a0a3c1d2d3643a815c5f9c0261e62\"\/>\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>The next example demonstrates:<\/p>\n<ul>\n<li>By calling <code style=\"color: #000000\">input()<\/code> multiple times, you can access multiple lines of input. The first call to <code style=\"color: #000000\">input()<\/code> gets the first line, the second gets the second line, et cetera.<\/li>\n<li>The string given by <code style=\"color: #000000;font-style: inherit\">input()<\/code> can be converted to\u00a0an\u00a0<code style=\"color: #000000;font-style: inherit\">int<\/code>\u00a0or a\u00a0<code style=\"color: #000000;font-style: inherit\">float<\/code>\u00a0(like lesson 4)<\/li>\n<li>In the second test case, the third line of input is not read, because\u00a0<code>input()<\/code>\u00a0is only called twice.<\/li>\n<\/ul>\n<p><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>\u00a0<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\ninputString = input()\naFloat = float(inputString)\nprint(aFloat + 1)\nprint('Second line:', input())<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput1\">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='submit1' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch1\" value=\"Input Switch\" onclick=\"pbInputSwitch(1,'N')\" ><\/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=\"ef34ea7699ef225e55382098c6e6c5b5\"\/>\n<div id='pbresults1' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(1,\"N\");<\/script>\n<\/p>\n<p>From now on, most exercises allow the option of entering your own test input.\u00a0Try the following experiment: press the <strong>Enter input<\/strong> button above. Leave the input text box empty. Then, press <strong>Run test<\/strong>. You should get an error like<\/p>\n<pre>EOFError: EOF when reading a line<\/pre>The acronym <strong>EOF<\/strong> stands for <strong>E<\/strong>nd <span style=\"color: #000000\"><strong>O<\/strong>f <strong>F<\/strong>ile. This message literally means that the program called <code>input()<\/code> but\u00a0failed\u00a0to have any available input\u00a0to read.<\/span><\/p>\n<ul>\n<li>Usually in our lessons, input is provided automatically by the grader, so this error\u00a0could mean\u00a0that your program called <code>input()<\/code> too many times, running out and going past the end of the grader's input.<\/li>\n<li>However,\u00a0in the example error you just caused, the input was user-provided, and you chose to provide\u00a0<em>no<\/em> input, so the <em>first<\/em> call to <code>input()<\/code> was already too much.<\/li>\n<\/ul>\n<p>For the next exercise, you are asked to debug a program which is not working, and make it work. Note that the bug is not a typo, but rather a <em>logical bug<\/em>: the program was not correctly designed to do its job, so you must redesign it a bit.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox2'>\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'>Echo<\/span><\/div>Write a program that reads one line of input, and prints out that same line two times. For example, if the input is\u00a0<code>Echo<\/code>\u00a0the output should be <\/p>\n<pre>Echo<br\/>Echo<\/pre> Fix the broken sample solution given below. (Or, delete the whole sample solution and start from scratch.) \u00a0<a class=\"hintlink\"  id=\"hintlink3\">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='usercode2' id='usercode2'  cols=10 rows=20   class='pyboxCode RW'>\nprint(input())\nprint(input())\n<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput2\">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='defaultCode2' value='print(input())\\nprint(input())\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch2\" value=\"Input Switch\" onclick=\"pbInputSwitch(2,'N')\" ><\/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><select id='pbSelect2' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(2,'5.echo')\" >History<\/option>\n<option name='default' data-pbonclick=\"pbSetText(2,descape($('#defaultCode2').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(2);\" >Help<\/option>\n<\/select><\/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=\"c46092927452986086a5249a3c9a0295\"\/>\n<div id='pbresults2' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>pbInputSwitch(2,\"N\");<\/script>\n<\/p>\n<p>You can continue to the next lesson, which is about <code><code>if<\/code><\/code> statements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last lesson we discussed user input, but did not really explain how user input is obtained. In Python, the user types one line of input at a time. You should use the input() function to actually obtain the &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/5-input\/\">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-560","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/560"}],"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=560"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/560\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}