{"id":447,"date":"2021-03-17T08:47:28","date_gmt":"2021-03-17T08:47:28","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=430"},"modified":"2025-01-16T12:29:42","modified_gmt":"2025-01-16T12:29:42","slug":"input-validation","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/national-5\/input-validation\/","title":{"rendered":"Input Validation"},"content":{"rendered":"<p>Input validation is used to ensure the user can only type in valid values.<\/p>\n<p>See also pages on <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/nat4\/decisions\/\">decisions (if commands)<\/a> and <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/reference\/conditions\/\">conditions<\/a>.<\/p>\n<h1>General Algorithms<\/h1>\n<pre>get input from user\r\nwhile input is not valid\r\n    display error\r\n    get input from user\r\nend while<\/pre>\n<pre>repeat\r\n    get input from user\r\n    if input is not valid\r\n        display error\r\nend while<\/pre>\n<h1>Length Check<\/h1>\n<p>User must enter a string that has a minimum\/maximum number of characters.<\/p>\n<pre class=\"brush: python; highlight: [3]; title: ; notranslate\" title=\"\">#\r\ncomputer = input(&#039;Enter computer ID:&#039;)\r\nwhile len(computer) != 14:\r\n    print(&#039;Must have 14 characters&#039;)\r\n    computer = input(&#039;Enter computer ID:&#039;)\r\n#<\/pre>\n<pre class=\"brush: python; highlight: [3]; title: ; notranslate\" title=\"\">#\r\nwhile True:\r\n    computer = input(&#039;Enter computer ID:&#039;)\r\n    if len(computer) != 14:\r\n        print(&#039;Must have 14 characters&#039;)\r\n    else:\r\n        break\r\n#<\/pre>\n<h1>Range Check<\/h1>\n<p>User must enter a number that is bigger\/smaller than another number.<\/p>\n<pre class=\"brush: python; highlight: [3]; title: ; notranslate\" title=\"\"># \r\nquantity= input(&#039;Enter quantity:&#039;) \r\nwhile quantity &lt; 1: \r\n    print(&#039;Must be at least 1&#039;) \r\n    quantity= input(&#039;Enter quantity:&#039;) \r\n#<\/pre>\n<pre class=\"brush: python; highlight: [3]; title: ; notranslate\" title=\"\">#\r\nwhile True:\r\n    quantity= input(&#039;Enter quantity:&#039;) \r\n    if quantity &lt; 1: \r\n        print(&#039;Must be at least 1&#039;)\r\n    else:\r\n        break\r\n#<\/pre>\n<h1>Restricted Choice<\/h1>\n<p>User must enter a value that is in a list of valid strings or numbers<\/p>\n<pre class=\"brush: python; highlight: [3]; title: ; notranslate\" title=\"\">#\r\nhappy = input(&#039;Are you happy?&#039;)\r\nwhile happy !=&#039;Yes&#039; and happy!=&#039;No&#039;: \r\n    print(&#039;Enter Yes or No&#039;)\r\n    happy = input(&#039;Are you happy?&#039;)\r\n#<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"brush: python; highlight: [3]; title: ; notranslate\" title=\"\">#\r\nwhile True:\r\n    happy = input(&#039;Are you happy?&#039;) \r\n    if happy !=&#039;Yes&#039; and happy!=&#039;No&#039;: \r\n        print(&#039;Enter Yes or No&#039;)\r\n    else:\r\n        break\r\n#<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Input validation is used to ensure the user can only type in valid values. See also pages on decisions (if commands) and conditions. General Algorithms get input from user while input is not valid display error get input from user end while repeat get input from user if input is not valid display error end while Length Check User must enter a string that has a minimum\/maximum number of characters.<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/national-5\/input-validation\/\">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-447","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/447","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=447"}],"version-history":[{"count":19,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/447\/revisions"}],"predecessor-version":[{"id":759,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/447\/revisions\/759"}],"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=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}