{"id":45902,"date":"2025-07-01T14:00:36","date_gmt":"2025-07-01T14:00:36","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=45902"},"modified":"2025-07-01T14:00:39","modified_gmt":"2025-07-01T14:00:39","slug":"in-the-file-num_finder-py-try-to-guess-a-number-between-1-and-5-while-getting-input-from-the-user","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/in-the-file-num_finder-py-try-to-guess-a-number-between-1-and-5-while-getting-input-from-the-user\/","title":{"rendered":"In the file num_finder.py, try to guess a number between 1 and 5 while getting input from the user."},"content":{"rendered":"\n<p>In the file num_finder.py, try to guess a number between 1 and 5 while getting input from the user. The best way to find it is to guess the number in the middle (i.e 3) and if it is not correct, check to see if it is bigger or smaller and then repeat the process. Do not worry about invalid input. Give the user a hint about valid input. Example runs: I will try to guess a number you choose between 1 and 5. Is it 3?(y\/n): y I win! Your number was 3. I will try to guess a number you choose between 1 and 5. Is it 3?(y\/n): n Is it bigger or smaller than 3?(b\/s): s Is it 2?(y\/n): y I win! Your number was 2. I will try to guess a number you choose between 1 and 5. Is it 3?(y\/n): n Is it bigger or smaller than 3?(b\/s): b Is it 4?(y\/n): n Your number has to be 5. Otherwise, you lied. Rubric: The first guess (3) runs (10 points) The second guess (2,4) after checking bigger or smaller runs (20 points) The final guess (1,5) runs (5 points) The user is provided with a hint for input (y\/n, b\/s) (5 points)<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Here&#8217;s a Python program that implements the number guessing game as described. The program guesses the number between 1 and 5 based on user input. If the initial guess is incorrect, it asks whether the number is larger or smaller, and continues narrowing down the range until the correct number is found.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopyEdit<code>def guess_number():\n    print(\"I will try to guess a number you choose between 1 and 5.\")\n    \n    # Start with the middle guess, which is 3\n    low = 1\n    high = 5\n    guess = 3\n\n    while True:\n        # Ask if the guess is correct\n        response = input(f\"Is it {guess}? (y\/n): \").strip().lower()\n        \n        if response == 'y':\n            print(f\"I win! Your number was {guess}.\")\n            break\n        elif response == 'n':\n            # Ask if the number is bigger or smaller\n            direction = input(\"Is it bigger or smaller than 3? (b\/s): \").strip().lower()\n            if direction == 'b':\n                low = guess + 1\n            elif direction == 's':\n                high = guess - 1\n            \n            # Adjust the next guess based on the updated range\n            guess = (low + high) \/\/ 2\n            \n            if low == high:\n                print(f\"Your number has to be {low}. Otherwise, you lied.\")\n                break\n        else:\n            print(\"Please enter valid input (y\/n or b\/s).\")\n\n# Call the function to play the game\nguess_number()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initial Setup:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The program starts with an initial guess of 3, which is the middle number between 1 and 5.<\/li>\n\n\n\n<li>Variables <code>low<\/code> and <code>high<\/code> are initialized to 1 and 5, respectively, to represent the possible range of numbers.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>User Input Loop:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The program repeatedly asks the user whether the current guess (3, and later adjusted) is correct. If the guess is correct (<code>y<\/code>), the program announces the win and stops.<\/li>\n\n\n\n<li>If the guess is incorrect (<code>n<\/code>), the program then asks the user if the number is bigger or smaller than the current guess (<code>b\/s<\/code>). Depending on the response, it adjusts the range for the next guess:\n<ul class=\"wp-block-list\">\n<li>If the number is bigger, the <code>low<\/code> range is updated to be the current guess + 1.<\/li>\n\n\n\n<li>If the number is smaller, the <code>high<\/code> range is updated to be the current guess &#8211; 1.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Adjusting the Guess:<\/strong>\n<ul class=\"wp-block-list\">\n<li>After each input (whether the number is bigger or smaller), the next guess is recalculated as the middle point of the new <code>low<\/code> and <code>high<\/code> range using the formula: <code>guess = (low + high) \/\/ 2<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Edge Case Handling:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the program reaches a point where <code>low == high<\/code>, it assumes that the remaining number is the correct one and announces it. If this is not the case (i.e., the user lied), it prints a message to that effect.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Input Validation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The program checks for valid input (<code>y\/n<\/code> for yes\/no, and <code>b\/s<\/code> for bigger\/smaller) and prompts the user again if the input is invalid.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Output Example:<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">First Run (Correct Guess on First Try):<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">cssCopyEdit<code>I will try to guess a number you choose between 1 and 5.\nIs it 3? (y\/n): y\nI win! Your number was 3.\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Second Run (Guessing After Getting &#8220;Smaller&#8221;):<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">vbnetCopyEdit<code>I will try to guess a number you choose between 1 and 5.\nIs it 3? (y\/n): n\nIs it bigger or smaller than 3? (b\/s): s\nIs it 2? (y\/n): y\nI win! Your number was 2.\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Third Run (Guessing After Getting &#8220;Bigger&#8221; and then &#8220;Smaller&#8221;):<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">vbnetCopyEdit<code>I will try to guess a number you choose between 1 and 5.\nIs it 3? (y\/n): n\nIs it bigger or smaller than 3? (b\/s): b\nIs it 4? (y\/n): n\nYour number has to be 5. Otherwise, you lied.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Scoring Breakdown (According to Rubric):<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>First Guess (3):<\/strong> Works correctly. (10 points)<\/li>\n\n\n\n<li><strong>Second Guess (2 or 4):<\/strong> After determining if the number is bigger or smaller, it correctly guesses 2 or 4. (20 points)<\/li>\n\n\n\n<li><strong>Final Guess (1 or 5):<\/strong> The program handles the final guess and correctly identifies the number. (5 points)<\/li>\n\n\n\n<li><strong>User Input Hint:<\/strong> The program provides hints for valid inputs (<code>y\/n<\/code>, <code>b\/s<\/code>). (5 points)<\/li>\n<\/ol>\n\n\n\n<p>By following this approach, the program efficiently narrows down the number and guesses correctly within three attempts, while providing clear input prompts and feedback.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"1024\" src=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner8-46.jpeg\" alt=\"\" class=\"wp-image-45903\" srcset=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner8-46.jpeg 852w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner8-46-250x300.jpeg 250w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner8-46-768x923.jpeg 768w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the file num_finder.py, try to guess a number between 1 and 5 while getting input from the user. The best way to find it is to guess the number in the middle (i.e 3) and if it is not correct, check to see if it is bigger or smaller and then repeat the process. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-45902","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/45902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=45902"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/45902\/revisions"}],"predecessor-version":[{"id":45904,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/45902\/revisions\/45904"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=45902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=45902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=45902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}