{"id":39287,"date":"2016-11-24T07:35:28","date_gmt":"2016-11-24T13:35:28","guid":{"rendered":"http:\/\/carolinasblogsite.wordpress.com\/?p=641"},"modified":"2016-11-24T07:35:28","modified_gmt":"2016-11-24T13:35:28","slug":"validating-user-input","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/validating-user-input\/","title":{"rendered":"Validating user input"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/carolinasblogsite.wordpress.com\/2016\/11\/24\/validating-user-input\/\"><a href=\"https:\/\/carolinasblogsite.wordpress.com\">Carolina&amp;#039;s Blog Site<\/a><\/a><\/em><br \/><\/p>\n<p>So when you ask the user for input, you need it to give you a valid input, something you can do whatever it is you wanted to do with it. For example, let&#8217;s say you ask for the user&#8217;s age because you wanna add something to it. Well, the user might write bdjsfkfs, you never know! So you must prepare accordingly.<\/p>\n<p>First of all, be sure to specify when asking the user for input. Do not assume they know what you want from them. Have the program say things like &#8220;only integers, or rounded to the nearest whole number or in military time please.&#8221;<\/p>\n<figure style=\"width: 462px\" class=\"wp-caption alignnone\"><img src=\"https:\/\/media.giphy.com\/media\/aZ4sQUpybai5y\/giphy.gif\" alt=\"pink sweet minions despicable me agnes\" width=\"462\" height=\"250\" \/><figcaption class=\"wp-caption-text\"><a href=\"http:\/\/giphy.com\/gifs\/pink-sweet-despicable-me-aZ4sQUpybai5y\">source<\/a><\/figcaption><\/figure>\n<p>Now, when asking for input, it will always be given as a string, and you won&#8217;t be able to do math operations with it.<\/p>\n<p><img class=\"alignnone  wp-image-653\" src=\"https:\/\/carolinasblogsite.files.wordpress.com\/2016\/11\/capture.jpg?w=500&#038;h=101\" alt=\"capture\" width=\"500\" height=\"101\" srcset=\"https:\/\/carolinasblogsite.files.wordpress.com\/2016\/11\/capture.jpg 396w, https:\/\/carolinasblogsite.files.wordpress.com\/2016\/11\/capture.jpg?w=150&amp;h=30 150w, https:\/\/carolinasblogsite.files.wordpress.com\/2016\/11\/capture.jpg?w=300&amp;h=61 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>One thing you must do is transform the user input into a number by using the int function. That way Python will automatically turn it into an integer and will be able to do math operations with it. Example:<\/p>\n<pre class=\"lang-py prettyprint prettyprinted\"><code><span class=\"pln\">age <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> int<\/span><span class=\"pun\">(<\/span><span class=\"pln\">input<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Please enter your age: \"<\/span><span class=\"pun\">))<\/span><\/code><\/pre>\n<p>Another great thing you can use in Python is the try\/except statement, like this:<\/p>\n<pre class=\"lang-py prettyprint prettyprinted\"><code><span class=\"kwd\">def<\/span><span class=\"pln\"> valid_user_input<\/span><span class=\"pun\">(<\/span><span class=\"pln\">x<\/span><span class=\"pun\">):<\/span>\n    <span class=\"kwd\">try<\/span><span class=\"pun\">:          #what you want it to do<\/span>\n        <span class=\"kwd\">return<\/span><span class=\"pln\"> int<\/span><span class=\"pun\">(<\/span><span class=\"pln\">x<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">&gt;<\/span> <span class=\"lit\">2<\/span>\n    <span class=\"kwd\">except<\/span> <span class=\"typ\">ValueError<\/span><span class=\"pun\">:     #if an error happens,<\/span>\n        <span class=\"kwd\">print (\"please type an integer\")     #do this\n        return None\n\n<\/span><\/code><\/pre>\n<p>&nbsp;<\/p><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/carolinasblogsite.wordpress.com\/641\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/carolinasblogsite.wordpress.com\/641\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=carolinasblogsite.wordpress.com&#038;blog=115369299&%23038;post=641&%23038;subd=carolinasblogsite&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Carolina&amp;#039;s Blog Site So when you ask the user for input, you need it to give you a valid input, something you can do whatever it is you wanted to do with it. For example, let&#8217;s say you ask for the user&#8217;s age because you wanna add something to it. Well, the &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/validating-user-input\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Validating user input<\/span><\/a><\/p>\n","protected":false},"author":30,"featured_media":39288,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[33],"class_list":["post-39287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","category-syndicated","tag-tc101"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/39287","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\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=39287"}],"version-history":[{"count":14,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/39287\/revisions"}],"predecessor-version":[{"id":42863,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/39287\/revisions\/42863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media\/39288"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=39287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=39287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=39287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}