{"id":529,"date":"2020-03-31T17:56:33","date_gmt":"2020-03-31T17:56:33","guid":{"rendered":"https:\/\/computerscienced.co.uk\/site\/?page_id=529"},"modified":"2021-08-03T21:36:51","modified_gmt":"2021-08-03T21:36:51","slug":"ks4-puzzle-10-import-from-text-file","status":"publish","type":"page","link":"https:\/\/computerscienced.co.uk\/site\/parsons-puzzles\/ks4-puzzle-10-import-from-text-file\/","title":{"rendered":"KS4 Puzzle 10 &#8211; import from text file"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Code \/ algorithm aim<\/h4>\n\n\n\n<p>The program should declare a list of fruit then display everything in the list on the screen.<\/p>\n\n\n\n<p>The program should then open a text file called fruits and import the lines of text into the list printing each line of text as it imports it.  <\/p>\n\n\n\n<p>The program should close the text file then display to the user the new list with the added items.        <\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<script>\nvar initial = 'fruits =[\"apple\",\"orange\",\"cherry\"]\\n' +\n                      'for i in fruits:\\n' +\n                      '  print(i)\\n' +\n                      'file=open(\"fruits.txt\",\"r\")\\n' +\n                      'for j in file:\\n' +\n                      '  j = j.rstrip(\"&bsol;n\")\\n' +\n                      '  print(j)\\n' +\n                      '  fruits.append(j)\\n' +\n                      'file.close()\\n' +\n                      'for i in fruits:\\n' +\n                      '  print(i)\\n';\n\nvar repllink = \"https:\/\/repl.it\/@MHelliwell\/KS4-Puzzle-10\";\n<\/script>\n<link href=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/parsons.css\" rel=\"stylesheet\">\n        <link href=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/lib\/prettify.css\" rel=\"stylesheet\">\n        <script src=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/lib\/prettify.js\"><\/script>\n<div id = \"help\" onclick=\"#\">\n<h4>How does the puzzle work?<\/h4>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/GuYSh-QSfRg\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<p>Simply drag the code blocks from the left to the right and put them in the correct order.<\/p>\n<p>Remember that some blocks will need to be indented just like in Python &#8211; to indent the block simply drag the block further to the right of the code above<\/p>\n<p>If you think you have the order right click on the &#8220;Check your answer&#8221; button.<\/p>\n<p>If you didn&#8217;t get it right you will be provided with some hints so you can try again.<\/p>\n<p><b>To close this prompt click on it or press the Instructions button again<\/b><\/p>\n<\/div>\n<div id = \"congrats\">\n<h3>Well done! You did it in <a id=\"clicks\">0<\/a> attempt(s)<\/h3>\nCheck out and run the code in repl:\n<a target=\"_blank\" id=\"repllink\" href=\"\" rel=\"noopener noreferrer\">Parsons Puzzle code<\/a>\n<h4>What does the code do?<\/h4>\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\">fruits <span style=\"color: #333333\">=<\/span>[<span style=\"background-color: #fff0f0\">&quot;apple&quot;<\/span>,<span style=\"background-color: #fff0f0\">&quot;orange&quot;<\/span>,<span style=\"background-color: #fff0f0\">&quot;cherry&quot;<\/span>]\n<span style=\"color: #008800; font-weight: bold\">for<\/span> i <span style=\"color: #000000; font-weight: bold\">in<\/span> fruits:\n  <span style=\"color: #007020\">print<\/span>(i)\nfile<span style=\"color: #333333\">=<\/span><span style=\"color: #007020\">open<\/span>(<span style=\"background-color: #fff0f0\">&quot;fruits.txt&quot;<\/span>,<span style=\"background-color: #fff0f0\">&quot;r&quot;<\/span>)\n<span style=\"color: #008800; font-weight: bold\">for<\/span> j <span style=\"color: #000000; font-weight: bold\">in<\/span> file:\n  j <span style=\"color: #333333\">=<\/span> j<span style=\"color: #333333\">.<\/span>rstrip(<span style=\"background-color: #fff0f0\">&#39;<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0\">\\n<\/span><span style=\"background-color: #fff0f0\">&#39;<\/span>)\n  <span style=\"color: #007020\">print<\/span>(j)\n  fruits<span style=\"color: #333333\">.<\/span>append(j)\nfile<span style=\"color: #333333\">.<\/span>close()\n<span style=\"color: #008800; font-weight: bold\">for<\/span> i <span style=\"color: #000000; font-weight: bold\">in<\/span> fruits:\n  <span style=\"color: #007020\">print<\/span>(i)\n<\/pre><\/td><\/tr><\/table><\/div>\n\n\n\n<p><b>Line 1<\/b> defines our list called fruits. We declare 3 items in our list apple, orange, cherry.  <\/p> \n<p><b>Line 2<\/b> is looking for all the items we have in our list. Every time it finds a value it is storing it as a temporary variable called &#8220;i&#8221; then goes to the next instruction. If it cannot find any more values in our list it will stop the for loop. <\/p> \n<p><b>Line 3<\/b> is displaying whatever value has been found in our list which has been saved to the temporary value of &#8220;i&#8221;.  The for loop will continue displaying every item in our list until it gets to the end and then the for loop will end. <\/p>\n<p><b>Line 4<\/b> is opening a text file called fruits.txt and assigning it to a variable called file.  The &#8220;r&#8221; indicates that the file can only be read.<\/p>\n<p><b>Line 5<\/b> is looking for all the lines of text we have in our file. Every time it finds a line it is storing it as a temporary variable called &#8220;j&#8221; then goes to the next instruction. If it cannot find any more values in our list it will stop the for loop. <\/p>\n<p><b>Line 6<\/b> is stripping out (getting rid of) the line breaks in the .txt file.  Even though we cannot see them in the file every time there is a new line there is code that looks like this &#8220;\\n&#8221;. If we do not get rid of this we will import it. <\/p>\n<p><b>Line 7<\/b> displays the line of text that our code has extracted from the text file and put into the temporary variable j. <\/p>\n<p><b>Line 8<\/b> adds whatever is saved in temporary variable j to the end of our fruits list.<\/p>\n<p><b>Line 9<\/b> closes the text file now that we have read from it. It is always important to close the file once we are done.<\/p>  \n<p><b>Line 10<\/b> is looking for all the items we have in our list. Every time it finds a value it is storing it as a temporary variable called &#8220;i&#8221; then goes to the next instruction. If it cannot find any more values in our list it will stop the for loop.<\/p>  \n<p><b>Line 11<\/b> is displaying whatever value has been found in our list which has been saved to the temporary value of &#8220;i&#8221;.  The for loop will continue displaying every item in our list until it gets to the end and then the for loop will end.  <\/p>       \n<\/div>\n<div id=\"container\">  \n<p><button class=\"dropbtn\" id=\"helpbuttontop\" onclick=\"#\">Instructions<\/button>   \n<button class=\"dropbtn\" id=\"newInstanceLinktop\" onclick=\"#\">Reset the puzzle<\/button>\n<button class=\"dropbtn\" id=\"feedbackLinktop\" onclick=\"#\">Check your answer<\/button>  <\/p>        \n<div id=\"sortableTrash\" class=\"sortable-code\"><\/div>\n        <div id=\"sortable\" class=\"sortable-code\">\n        <\/div>\n<\/div>\n        <div style=\"clear:both;\"><\/div>\n        <p>\n<button class=\"dropbtn\" id=\"helpbutton\" onclick=\"#\">Instructions<\/button>   \n<button class=\"dropbtn\" id=\"newInstanceLink\" onclick=\"#\">Reset the puzzle<\/button>\n   <button class=\"dropbtn\" id=\"feedbackLink\" onclick=\"#\">Check your answer<\/button>\n\n<p><b>You have tried this : <\/b><a id=\"clicksx\">0<\/a> time(s)<\/p>         \n<!-- <a href=\"#\" id=\"newInstanceLink\">Reset the puzzle<\/a> -->\n            <!-- <a href=\"#\" id=\"feedbackLink\">Check your answer<\/a> -->\n        <\/p>\n        <script src=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/lib\/jquery.min.js\"><\/script>\n        <script src=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/lib\/jquery-ui.min.js\"><\/script>\n        <script src=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/lib\/jquery.ui.touch-punch.min.js\"><\/script>\n        <script src=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/lib\/underscore-min.js\"><\/script>\n        <script src=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/lib\/lis.js\"><\/script>\n        <script src=\"https:\/\/computerscienced.co.uk\/site\/js-parsons\/parsons.js\"><\/script>\n        <script>\ndocument.getElementById(\"repllink\").href = repllink;\nvar clicks = 0;\nvar clicksx = 0;  \nvar x = document.getElementById(\"congrats\");\nvar y = document.getElementById(\"help\"); \n\n                     \n\n        function displayErrors(fb) {\n            if(fb.errors.length > 0) {\n                alert(fb.errors[0]);\n            }\n        } \n\n        $(document).ready(function(){\n            var parson = new ParsonsWidget({\n                'sortableId': 'sortable',\n                'trashId': 'sortableTrash',\n                'max_wrong_lines': 1,\n                'feedback_cb' : displayErrors\n            });\n            parson.init(initial);\n            parson.shuffleLines();\n            $(\"#newInstanceLink\").click(function(event){\n                clicks = 0;\n                clicksx = 0;\n                document.getElementById(\"clicks\").innerHTML = clicks;\n                document.getElementById(\"clicksx\").innerHTML = clicksx;\n                x.style.display = \"none\";\n                y.style.display = \"none\";\n                event.preventDefault();\n                parson.shuffleLines();\n            });\n$(\"#newInstanceLinktop\").click(function(event){\n                clicks = 0;\n                clicksx = 0;\n                document.getElementById(\"clicks\").innerHTML = clicks;\n                document.getElementById(\"clicksx\").innerHTML = clicksx;\n                x.style.display = \"none\";\n                y.style.display = \"none\";\n                event.preventDefault();\n                parson.shuffleLines();\n            });\n            $(\"#helpbutton\").click(function(event){\n             if (y.style.display === \"block\") {\n    y.style.display = \"none\";\n  } else {\n    y.style.display = \"block\";\n  }\n            });\n$(\"#helpbuttontop\").click(function(event){\n             if (y.style.display === \"block\") {\n    y.style.display = \"none\";\n  } else {\n    y.style.display = \"block\";\n  }\n            });\n $(\"#help\").click(function(event){\n             if (y.style.display === \"block\") {\n    y.style.display = \"none\";\n  } else {\n    y.style.display = \"block\";\n  }\n            });\n            $(\"#feedbackLink\").click(function(event){\n                clicks += 1;\n                clicksx += 1;\n                document.getElementById(\"clicks\").innerHTML = clicks;\n                document.getElementById(\"clicksx\").innerHTML = clicksx;\n                event.preventDefault();\n                parson.getFeedback();\n            });\n$(\"#feedbackLinktop\").click(function(event){\n                clicks += 1;\n                clicksx += 1;\n                document.getElementById(\"clicks\").innerHTML = clicks;\n                document.getElementById(\"clicksx\").innerHTML = clicksx;\n                event.preventDefault();\n                parson.getFeedback();\n            });\n        });\n        <\/script>\n\n\n\n<p class=\"has-light-gray-background-color has-background\">Finished KS4 Puzzle 10?&nbsp;Go back and&nbsp;<strong><a href=\"https:\/\/computerscienced.co.uk\/site\/parsons-puzzles\/\">choose another Parsons puzzle.<\/a><\/strong><\/p>\n\n\n\n<p>This code is based on js-parsons available at  <a href=\"https:\/\/github.com\/js-parsons\/js-parsons\">https:\/\/github.com\/js-parsons\/js-parsons<\/a> and distributed under an MIT license<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code \/ algorithm aim The program should declare a list of fruit then display everything in the list on the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":224,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"disable_featured_image":false,"footnotes":""},"class_list":["post-529","page","type-page","status-publish","hentry"],"blog_post_layout_featured_media_urls":{"thumbnail":"","full":""},"categories_names":null,"comments_number":"0","_links":{"self":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/comments?post=529"}],"version-history":[{"count":17,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/529\/revisions"}],"predecessor-version":[{"id":1811,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/529\/revisions\/1811"}],"up":[{"embeddable":true,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/224"}],"wp:attachment":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/media?parent=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}