{"id":38111,"date":"2016-10-22T18:23:24","date_gmt":"2016-10-22T23:23:24","guid":{"rendered":"https:\/\/kenscourses.com\/tc101fall2016\/?guid=0cd4b2450e0c70b31f51ccc77cd356cc"},"modified":"2016-10-22T18:23:24","modified_gmt":"2016-10-22T23:23:24","slug":"your-argument-is-invalid-isnt-it","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/your-argument-is-invalid-isnt-it\/","title":{"rendered":"YOUR ARGUMENT IS INVALID, isn&#8217;t it?"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"http:\/\/emnl.me\/your-argument-is-invalid-isnt-it\/\"><a href=\"http:\/\/emnl.me\/\">Coding The Future<\/a><\/a><\/em><br \/><\/p>\n<p><iframe src=\"http:\/\/giphy.com\/embed\/l0MYyiDqMf6lv527u\" width=\"100%\" height=\"261\" frameborder=\"0\" class=\"giphy-embed\" allowfullscreen><\/iframe><\/p><p><a href=\"http:\/\/giphy.com\/gifs\/election2016-election-2016-presidential-debate-l0MYyiDqMf6lv527u\">via GIPHY<\/a><\/p><p><\/p>\n\n<p>If you had been in my house while I was watching the US Presidential Debates you would probably had heard me scream to my computer \"YOUR ARGUMENT IS INVALID DONALD!\". Not only do I hate that guy for being so racist, but also because everything he says is just so dumb. And then my girl Hillary comes in saying that you can fact-check Donald from her website in real time. Gotta love that QUEEN!<\/p>\n\n<p>But aside from US politics, let's move on to today's topic: <strong>validating user input<\/strong>. This will not exactly help us to fact-check Donald, but to make sure that the user is entering the right kind of information.<\/p>\n\n<p>Let's take a registration form for example. We want to collect the user's name, email, and phone number. Therefore, we must make sure that the user includes an @ and a .something and that only numbers are inputted in the phone number field. Here's how it works...<\/p>\n\n<h4 id=\"usingthetryapproach\">Using the \"Try\" Approach<\/h4>\n\n<p>To validate input, we could use if-statements, but there's a more effective approach commonly known as <em>Try-catch<\/em>. This method tries to do something, and if it can't, instead of crashing the program, makes it do something else.<\/p>\n\n<p>Take a look at the following sample code:<\/p>\n\n<p><code>try:\n   phoneNum = int(input(\"Please enter your phone #: \"))\n except ValueError:\n   print(\"Sorry, that phone number is not valid.\")<\/code><\/p>\n\n<p>As you can see, the first step was to type <em>try<\/em> followed by the code you need to validate, which in this case is making sure the input only whole numbers, and then using an <em>except<\/em> statement to break out of if an error was returned. This prevents the program from crashing, and if combined with a while loop, can prompt the user to enter their number again until they get <!--more--> right.<\/p>\n\n<h4 id=\"validatingemail\">Validating email<\/h4>\n\n<p>Email validation can be a little harder, but a little easier at the same time. This is because the easiest way to do so is using a library called <a href=\"https:\/\/pypi.python.org\/pypi\/validate_email\"><em>validate_email<\/em><\/a> that can be installed from your terminal.<\/p>\n\n<p>Once installed, it is super easy to use. All you need to do is call the <em>validate_email()<\/em> function.<\/p>\n\n<p><code>is_valid = validate_email('example@example.com')<\/code><\/p>\n\n<p>The setup instructions for this module can be found <a href=\"https:\/\/pypi.python.org\/pypi\/validate_email\">here<\/a>.<\/p>\n\n<p>That's it for now... And get exited, because I will be talking about my first Twitter Bot and how to build your own next week!<\/p>\n\n<p><a href=\"http:\/\/twitter.com\/emamex98\">@emamex98<\/a><\/p>\n\n<div id=\"disqus_thread\"><\/div>  \n\n<script type=\"text\/javascript\">  \n    var disqus_shortname = 'emnl-me';\n    var disqus_identifier = 'valid-input';\n\n    (function() {\n        var dsq = document.createElement('script'); dsq.type = 'text\/javascript'; dsq.async = true;\n        dsq.src = '\/\/' + disqus_shortname + '.disqus.com\/embed.js';\n        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);\n    })();\n<\/script>  \n\n<noscript>Please enable JavaScript to view the <a href=\"http:\/\/disqus.com\/?ref_noscript\">comments powered by Disqus.<\/a><\/noscript>  \n\n<p><a href=\"http:\/\/disqus.com\/\" class=\"dsq-brlink\">comments powered by <span class=\"logo-disqus\">Disqus<\/span><\/a> <\/p>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Coding The Future via GIPHY If you had been in my house while I was watching the US Presidential Debates you would probably had heard me scream to my computer &#8220;YOUR ARGUMENT IS INVALID DONALD!&#8221;. Not only do I hate that guy for being so racist, but also because everything he says &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/your-argument-is-invalid-isnt-it\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">YOUR ARGUMENT IS INVALID, isn&#8217;t it?<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[294,149,125,45,48,49,293,292],"class_list":["post-38111","post","type-post","status-publish","format-standard","hentry","category-blogs","category-syndicated","tag-catch","tag-elif","tag-else","tag-if-statements","tag-input","tag-output","tag-try","tag-validation"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=38111"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38111\/revisions"}],"predecessor-version":[{"id":38112,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/38111\/revisions\/38112"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=38111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=38111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=38111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}