{"id":409,"date":"2024-07-15T16:48:37","date_gmt":"2024-07-15T11:48:37","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=409"},"modified":"2024-10-05T09:06:01","modified_gmt":"2024-10-05T04:06:01","slug":"python-user-input","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-user-input\/","title":{"rendered":"Python: User input"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>Input from Keyboard<\/li>\n\n\n\n<li>For this purpose, Python provides the function <strong>input()<\/strong>. input has an optional parameter, which is the prompt string.<\/li>\n<\/ul>\n\n\n\n<p>When taking input, use the <strong>input()<\/strong> function. When a program executes on the console, it <strong>asks the user for input<\/strong>. It&#8217;s important to remember that any input taken from the user, <strong>whether it&#8217;s a number or an alphabet, will be stored in string format<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important points<\/h3>\n\n\n\n<ul class=\"is-style-checkmark-list wp-block-list\">\n<li>When working with user input, it&#8217;s important to <strong>validate and sanitize the input<\/strong> to ensure it&#8217;s in the expected format and doesn&#8217;t introduce security vulnerabilities into your program.<\/li>\n\n\n\n<li>To convert user input to a different data type, such as an integer or a float, you can use type casting. <strong>For example,<\/strong> if you want to convert a user input to an integer, you can use the <strong>int()<\/strong> function.<\/li>\n\n\n\n<li>It&#8217;s important to handle exceptions when working with user input. <strong>For example<\/strong>, if the user enters a string when you&#8217;re expecting a number, you&#8217;ll want to handle that gracefully to avoid your program crashing.<\/li>\n\n\n\n<li>Always provide clear instructions to the user when asking for input. This helps users understand what type of input is expected and reduces the likelihood of errors.<\/li>\n<\/ul>\n\n\n\n<div style=\"height:5rem\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.612), 1.5rem);\"><code><strong>username = input(\"Enter username:\")<\/strong><\/code><\/pre>\n\n\n\n<div style=\"height:5rem\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">For example<\/h3>\n\n\n\n<p>When you input the number <strong>5<\/strong>, if you store it in a variable, it <strong>will be stored as a string, not an integer<\/strong>. When you take integer input from users and save it as an integer if it is an integer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When taking input, use the input() function. When a program executes on the console, it asks the user for input. It&#8217;s important to remember that any input taken from the user, whether it&#8217;s a number or an alphabet, will be stored in string format. Important points For example When you input the number 5, if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-409","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":8,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/409\/revisions\/417"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}