{"id":162,"date":"2021-03-14T17:31:24","date_gmt":"2021-03-14T17:31:24","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=162"},"modified":"2023-02-07T09:12:13","modified_gmt":"2023-02-07T09:12:13","slug":"input-values-into-arrays","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/national-5\/input-values-into-arrays\/","title":{"rendered":"Input Values into Arrays"},"content":{"rendered":"<p>The values for an array might have to be typed in when the program is running:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#Create empty arrays of correct type and size \r\nnames = &#x5B;&quot;&quot;] * 20\r\nscores = &#x5B;0] * 20\r\n\r\n#Count through the array indexes\r\nfor index in range(20):\r\n    #ask for and save value in the array\r\n    names&#x5B;index] = input(&quot;Enter name:&quot;))\r\n    scores&#x5B;index] = int(input(&quot;Enter score:&quot;))\r\n#<\/pre>\n<h1>Input Validation<\/h1>\n<p>This uses the same standard algorithm for <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/input-validation\/\">input validation of variable<\/a>.<\/p>\n<p>However, each reference to the variable becomes a reference to the array and its index value (lines 7 and 8).<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nfor index in range(20):\r\n    names&#x5B;index] = input(&quot;Enter name:&quot;)) \r\n\r\n    # get valid score\r\n    while True:\r\n        scores&#x5B;index] = int(input(&quot;Enter score:&quot;))\r\n        if scores&#x5B;index] &lt; 0:\r\n     \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print('Must be at least 1')\r\n    \u00a0\u00a0\u00a0\u00a0else:\r\n            break \r\n#<\/pre>\n<h1><\/h1>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The values for an array might have to be typed in when the program is running: #Create empty arrays of correct type and size names = &#x5B;&quot;&quot;] * 20 scores = &#x5B;0] * 20 #Count through the array indexes for index in range(20): #ask for and save value in the array names&#x5B;index] = input(&quot;Enter name:&quot;)) scores&#x5B;index] = int(input(&quot;Enter score:&quot;)) # Input Validation This uses the same standard algorithm for input<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/national-5\/input-values-into-arrays\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":337,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-162","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":5,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/162\/revisions"}],"predecessor-version":[{"id":742,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/162\/revisions\/742"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/337"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}