{"id":737,"date":"2020-10-23T07:20:33","date_gmt":"2020-10-23T07:20:33","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=737"},"modified":"2025-03-31T04:14:48","modified_gmt":"2025-03-31T04:14:48","slug":"python-do-while","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-do-while\/","title":{"rendered":"Python do&#8230;while Loop Statement Emulation"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to emulate the <code>do...while<\/code> loop statement in Python<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-do-while-loop-statement'>Introduction to the do&#8230;while loop statement <a href=\"#introduction-to-the-do-while-loop-statement\" class=\"anchor\" id=\"introduction-to-the-do-while-loop-statement\" title=\"Anchor for Introduction to the do...while loop statement\">#<\/a><\/h2>\n\n\n\n<p>If you have come from other programming languages such as <a href=\"https:\/\/www.javascripttutorial.net\/javascript-do-while\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a>, Java, or C#, you&#8217;re already familiar with the <code>do...while<\/code> loop statement.<\/p>\n\n\n\n<p>Unlike the <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-while\/\">while<\/a><\/code> loop, the <code>do...while<\/code> loop statement executes at least one iteration. It checks the <code>condition<\/code> at the end of each iteration and executes a code block until the <code>condition<\/code> is <code>False<\/code>.<\/p>\n\n\n\n<p>The following shows the pseudocode for the <code>do...while<\/code> loop in Python:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">do<\/span>\n    <span class=\"hljs-comment\"># code block<\/span>\n<span class=\"hljs-keyword\">while<\/span> condition<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Unfortunately, Python doesn&#8217;t support the <code>do...while<\/code> loop. However, you can use the <code>while<\/code> loop and a <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-break\/\">break<\/a><\/code> statement to emulate the <code>do...while<\/code> loop statement.<\/p>\n\n\n\n<p>First, specify the <code>condition<\/code> as <code>True<\/code> in the <code>while<\/code> loop like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-keyword\">True<\/span>:\n    <span class=\"hljs-comment\"># code block<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This allows the code block to execute for the first time. However, since the condition is always <code>True<\/code>, it creates an indefinite loop. This is not what we expected.<\/p>\n\n\n\n<p>Second, place a condition to break out of the <code>while<\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-keyword\">True<\/span>:\n    <span class=\"hljs-comment\"># code block<\/span>\n\n    <span class=\"hljs-comment\"># break out of the loop<\/span>\n    <span class=\"hljs-keyword\">if<\/span> condition\n        <span class=\"hljs-keyword\">break<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the code block always executes at least one for the first time and the condition is checked at the end of each iteration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-do-while-loop-emulation-example'>Python do&#8230;while loop emulation example <a href=\"#python-do-while-loop-emulation-example\" class=\"anchor\" id=\"python-do-while-loop-emulation-example\" title=\"Anchor for Python do...while loop emulation example\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you need to develop a number guessing game with the following logic:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, generate a random number within a range e.g., 0 to 10.<\/li>\n\n\n\n<li>Then, repeatedly prompt users for entering a number. If the entered number is lower or higher than the random number, give users a hint. If the entered number equals the random number, the loop stops.<\/li>\n<\/ul>\n\n\n\n<p>The following program uses a <code>while<\/code> loop to develop the number guessing game:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> random <span class=\"hljs-keyword\">import<\/span> randint\n\n<span class=\"hljs-comment\"># determine the range<\/span>\nMIN = <span class=\"hljs-number\">0<\/span>\nMAX = <span class=\"hljs-number\">10<\/span>\n\n<span class=\"hljs-comment\"># generate a secret number<\/span>\nsecret_number = randint(MIN, MAX)\n\n<span class=\"hljs-comment\"># initialize the attempt<\/span>\nattempt = <span class=\"hljs-number\">0<\/span>\n\n<span class=\"hljs-comment\"># The first attempt<\/span>\ninput_number = int(input(<span class=\"hljs-string\">f'Enter a number between <span class=\"hljs-subst\">{MIN}<\/span> and <span class=\"hljs-subst\">{MAX}<\/span>:'<\/span>))\nattempt += <span class=\"hljs-number\">1<\/span>\n\n<span class=\"hljs-keyword\">if<\/span> input_number &gt; secret_number:\n    print(<span class=\"hljs-string\">'It should be smaller.'<\/span>)\n<span class=\"hljs-keyword\">elif<\/span> input_number &lt; secret_number:\n    print(<span class=\"hljs-string\">'It should be bigger.'<\/span>)\n<span class=\"hljs-keyword\">else<\/span>:\n    print(<span class=\"hljs-string\">f'Bingo! <span class=\"hljs-subst\">{attempt}<\/span> attempt(s)'<\/span>)\n\n<span class=\"hljs-comment\"># From the second attempt<\/span>\n<span class=\"hljs-keyword\">while<\/span> input_number != secret_number:\n\n    input_number = int(input(<span class=\"hljs-string\">f'Enter a number between <span class=\"hljs-subst\">{MIN}<\/span> and <span class=\"hljs-subst\">{MAX}<\/span>:'<\/span>))\n    attempt += <span class=\"hljs-number\">1<\/span>\n\n    <span class=\"hljs-keyword\">if<\/span> input_number &gt; secret_number:\n        print(<span class=\"hljs-string\">'It should be smaller.'<\/span>)\n    <span class=\"hljs-keyword\">elif<\/span> input_number &lt; secret_number:\n        print(<span class=\"hljs-string\">'It should be bigger.'<\/span>)\n    <span class=\"hljs-keyword\">else<\/span>:\n        print(<span class=\"hljs-string\">f'Bingo! <span class=\"hljs-subst\">{attempt}<\/span> attempt(s)'<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows a sample run:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">Enter a number between 0 and 10:5\nIt should be bigger.\nEnter a number between 0 and 10:7\nIt should be bigger.\nEnter a number between 0 and 10:8\nBingo! 3 attempt(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the <code>while<\/code> loop checks for the condition at the beginning of each iteration, it&#8217;s necessary to repeat the code that prompts for user input and checking the number twice, one before the loop and one inside the loop.<\/p>\n\n\n\n<p>To avoid this duplicate code, you can use a <code>while<\/code> loop to emulate <code>do while<\/code> loop as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> random <span class=\"hljs-keyword\">import<\/span> randint\n\n<span class=\"hljs-comment\"># determine the range<\/span>\nMIN = <span class=\"hljs-number\">0<\/span>\nMAX = <span class=\"hljs-number\">10<\/span>\n\n<span class=\"hljs-comment\"># generate a secret number<\/span>\nsecret_number = randint(MIN, MAX)\n\n<span class=\"hljs-comment\"># initialize the attempt<\/span>\nattempt = <span class=\"hljs-number\">0<\/span>\n\n<span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n    attempt += <span class=\"hljs-number\">1<\/span>\n\n    input_number = int(input(<span class=\"hljs-string\">f'Enter a number between <span class=\"hljs-subst\">{MIN}<\/span> and <span class=\"hljs-subst\">{MAX}<\/span>:'<\/span>))\n\n    <span class=\"hljs-keyword\">if<\/span> input_number &gt; secret_number:\n        print(<span class=\"hljs-string\">'It should be smaller.'<\/span>)\n    <span class=\"hljs-keyword\">elif<\/span> input_number &lt; secret_number:\n        print(<span class=\"hljs-string\">'It should be bigger.'<\/span>)\n    <span class=\"hljs-keyword\">else<\/span>:\n        print(<span class=\"hljs-string\">f'Bingo! <span class=\"hljs-subst\">{attempt}<\/span> attempt(s)'<\/span>)\n        <span class=\"hljs-keyword\">break<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, remove the code before the <code>while<\/code> loop.<\/li>\n\n\n\n<li>Second, add the condition to stop the loop if the entered number equals the random number by using the <code>break<\/code> statement.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python doesn&#8217;t support the do-while loop statement.<\/li>\n\n\n\n<li>Use a <code>while<\/code> loop and the <code>break<\/code> statements to emulate a <code>do...while<\/code> loop in Python.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=do-while\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"737\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-do-while\/\"\n\t\t\t\tdata-post-title=\"Python do&#8230;while Loop Statement Emulation\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"737\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-do-while\/\"\n\t\t\t\tdata-post-title=\"Python do&#8230;while Loop Statement Emulation\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to emulate the do&#8230;while loop statement in Python Introduction to the do&#8230;while loop statement # If you have come from other programming languages such as JavaScript, Java, or C#, you&#8217;re already familiar with the do&#8230;while loop statement. Unlike the while loop, the do&#8230;while loop statement executes at least [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":51,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-737","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=737"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/737\/revisions"}],"predecessor-version":[{"id":7283,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/737\/revisions\/7283"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}