{"id":187599,"date":"2025-02-05T10:24:59","date_gmt":"2025-02-05T10:24:59","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=187599"},"modified":"2025-02-05T10:25:02","modified_gmt":"2025-02-05T10:25:02","slug":"python-5-2-2-basic-while-loop-with-user-input","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/05\/python-5-2-2-basic-while-loop-with-user-input\/","title":{"rendered":"Python 5.2.2: Basic while loop with user input"},"content":{"rendered":"\n<p>?Python 5.2.2: Basic while loop with user input.<\/p>\n\n\n\n<p>Write an expression that executes the loop body as long as the user enters a non-negative number.<\/p>\n\n\n\n<p>Note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report &#8220;Program end never reached.&#8221; The system doesn&#8217;t print the test case that caused the reported message.<\/p>\n\n\n\n<p>Sample outputs with inputs: 9 5 2 -1<\/p>\n\n\n\n<p>Body<\/p>\n\n\n\n<p>Body<\/p>\n\n\n\n<p>Body<\/p>\n\n\n\n<p>Done<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Correct Answer:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Get the first user input\nnum = int(input())\n\n# Loop runs as long as the number is non-negative\nwhile num &gt;= 0:\n    print(\"Body\")\n    num = int(input())  # Get the next input\n\nprint(\"Done\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation (300 Words)<\/h3>\n\n\n\n<p>A <strong>while loop<\/strong> in Python executes a block of code as long as a specified condition evaluates to <code>True<\/code>. In this case, we want the loop to continue running <strong>as long as the user enters a non-negative number (0 or positive values).<\/strong> The moment a negative number is entered, the loop should stop.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step-by-Step Breakdown:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Getting User Input:<\/strong><br>The first input is obtained using <code>input()<\/code>, and it&#8217;s converted to an integer using <code>int()<\/code>. This ensures that the program processes numerical values rather than string inputs.<\/li>\n\n\n\n<li><strong>Loop Condition (<code>while num >= 0<\/code>):<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The condition <code>num >= 0<\/code> ensures that the loop continues as long as the entered number is <strong>not negative<\/strong>.<\/li>\n\n\n\n<li>If the user enters <code>-1<\/code> or any other negative number, the condition evaluates to <code>False<\/code>, and the loop stops.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Printing &#8220;Body&#8221;:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inside the loop, the statement <code>print(\"Body\")<\/code> executes, indicating that the loop body has been processed.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Getting the Next Input:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>After printing &#8220;Body&#8221;, the program asks for another input using <code>num = int(input())<\/code>.<\/li>\n\n\n\n<li>This ensures that each time the user enters a non-negative number, the loop continues.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Exiting the Loop:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When the user enters a negative number, the loop condition fails.<\/li>\n\n\n\n<li>The program exits the loop and executes <code>print(\"Done\")<\/code>, signaling that the loop has terminated.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Sample Run:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Input: 9  \nOutput: Body  \nInput: 5  \nOutput: Body  \nInput: 2  \nOutput: Body  \nInput: -1  \nOutput: Done<\/code><\/pre>\n\n\n\n<p>This ensures that the program does <strong>not enter an infinite loop<\/strong> and properly stops when a negative number is encountered.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>?Python 5.2.2: Basic while loop with user input. Write an expression that executes the loop body as long as the user enters a non-negative number. Note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report &#8220;Program end never reached.&#8221; The system doesn&#8217;t print [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-187599","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/187599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=187599"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/187599\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=187599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=187599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=187599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}