{"id":187601,"date":"2025-02-05T10:58:10","date_gmt":"2025-02-05T10:58:10","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=187601"},"modified":"2025-02-05T14:58:18","modified_gmt":"2025-02-05T14:58:18","slug":"basic-while-loop-with-user-input","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/05\/basic-while-loop-with-user-input\/","title":{"rendered":"\u00a0Basic while loop with user input"},"content":{"rendered":"\n<p>&nbsp;Basic while loop with user input. \u00c3\u201a\u00c2&nbsp; 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 the test case that caused the reported message. Sample outputs with inputs: 9 5 2 -1 Body Body Body Done. \u00c3\u201a\u00c2&nbsp; Use Python, please. CHALLENGE\u00c3\u201a\u00c2&nbsp;ACTIVITY 5.2.2:\u00c3\u201a\u00c2&nbsp;Basic while loop with user input. \u00c3\u201a\u00c2&nbsp; 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 Body Body Body Done. \u00c3\u201a\u00c2&nbsp; Use Python, please.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/02\/image-38.png\" alt=\"\" class=\"wp-image-187602\"\/><\/figure>\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<p>To create a Python program that repeatedly prompts the user for input and executes a specific action as long as the user enters a non-negative number, you can utilize a <code>while<\/code> loop. The loop will continue to run until a negative number is entered, at which point it will terminate and print a concluding message. Here&#8217;s how you can implement this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    user_input = int(input())\n    if user_input &lt; 0:\n        break\n    print(\"Body\")\nprint(\"Done.\")<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Infinite Loop Initialization:<\/strong> <code>while True:<\/code> starts an infinite loop that will continue running until it&#8217;s explicitly broken. This is a common pattern when the number of iterations isn&#8217;t predetermined.<\/li>\n\n\n\n<li><strong>User Input:<\/strong> <code>user_input = int(input())<\/code> prompts the user to enter a number. The <code>input()<\/code> function reads the input as a string, and <code>int()<\/code> converts it to an integer. If the user enters a non-integer value, this will raise a <code>ValueError<\/code>. For a more robust program, you might want to include error handling to manage such cases, but for simplicity, it&#8217;s omitted here.<\/li>\n\n\n\n<li><strong>Condition Check:<\/strong> <code>if user_input &lt; 0:<\/code> checks if the entered number is negative. If it is, the <code>break<\/code> statement terminates the loop, and the program proceeds to the code following the loop.<\/li>\n\n\n\n<li><strong>Loop Body Execution:<\/strong> If the entered number is non-negative, <code>print(\"Body\")<\/code> executes, simulating the loop&#8217;s main action. In your specific case, it prints the string &#8220;Body&#8221;.<\/li>\n\n\n\n<li><strong>Loop Termination Message:<\/strong> After exiting the loop (when a negative number is entered), <code>print(\"Done.\")<\/code> executes, indicating the program has finished processing.<\/li>\n<\/ol>\n\n\n\n<p><strong>Sample Interaction:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>9\nBody\n5\nBody\n2\nBody\n-1\nDone.<\/code><\/pre>\n\n\n\n<p>In this interaction:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The user enters <code>9<\/code>, <code>5<\/code>, and <code>2<\/code>, each of which is non-negative, so the program prints &#8220;Body&#8221; after each entry.<\/li>\n\n\n\n<li>Upon entering <code>-1<\/code>, which is negative, the loop terminates, and the program prints &#8220;Done.&#8221;<\/li>\n<\/ul>\n\n\n\n<p><strong>Key Considerations:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input Validation:<\/strong> The current implementation assumes the user will enter valid integers. To handle invalid inputs gracefully, you can incorporate a <code>try-except<\/code> block to catch <code>ValueError<\/code> exceptions when the input cannot be converted to an integer.<\/li>\n\n\n\n<li><strong>Loop Control:<\/strong> Using <code>while True:<\/code> with a <code>break<\/code> statement is a common idiom for loops that wait for a specific condition to exit, especially when the number of iterations isn&#8217;t known in advance.<\/li>\n\n\n\n<li><strong>Code Readability:<\/strong> Ensure your code is well-commented and follows consistent indentation practices, as Python relies on indentation to define code blocks.<\/li>\n<\/ul>\n\n\n\n<p>By following this structure, you can create a loop that processes user input until a negative number is entered, at which point it terminates gracefully.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;Basic while loop with user input. \u00c3\u201a\u00c2&nbsp; 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 the [&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-187601","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/187601","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=187601"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/187601\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=187601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=187601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=187601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}