There is no do-while loop in Python.

This is a similar construct, taken from the link above.

 while True:
     do_something()
     if condition():
        break
Answer from theycallmemorty on Stack Overflow
Discussions

Until statement/loop python? - Stack Overflow
You don't really need to count how many times you're looping unless you're doing something with that variable. More on stackoverflow.com
🌐 stackoverflow.com
Why there is no do while loop in python - Stack Overflow
Because everyone is looking at it wrong. You don't want DO ... WHILE you want DO ... UNTIL. If the intitial condition is true, a WHILE loop is probably what you want. The alternative is not a REPEAT ... WHILE loop, it's a REPEAT ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to emulate a do-while loop? - Stack Overflow
I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work: list_of_ints = [ 1, 2, 3 ] iterator = list_of_ints.__iter__() element = None More on stackoverflow.com
🌐 stackoverflow.com
Python: Most efficient loop until condition - Stack Overflow
I have got to a point in my code where I need to do this: while True: if first_number == second_number: do_something() break The first number will keep incrementing beyond my control an... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 3
5

continue is a keyword that requires no arguments. It simply tells the current loop to continue immediately to the next iteration. It can be used inside of while and for loops.

Your code should then be placed within the while loop, which will keep going until the condition is met. Your condition syntax is not correct. It should read while response != 'exit':. Because you are using a condition, the continue statement is not needed. It will by design continue as long as the value is not "exit".

Your structure would then look like this:

response = ''
# this will loop until response is not "exit"
while response != 'exit':
    response = raw_input("foo")

If you wanted to make use of continue, it might be used if you were going to do other various operations on the response, and might need to stop early and try again. The break keyword is a similar way to act on the loop, but it instead says we should immediately end the loop completely. You might have some other condition that is a deal breaker:

while response != 'exit':
    response = raw_input("foo")

    # make various checks on the response value
    # obviously "exit" is less than 10 chars, but these
    # are just arbitrary examples
    if len(response) < 10:
        print "Must be greater than 10 characters!"
        continue  # this will try again 

    # otherwise
    # do more stuff here
    if response.isdigit():
        print "I hate numbers! Unacceptable! You are done."
        break
2 of 3
3

Your while loop will continue until the condition you've set is false. So you want your code to mostly be inside this loop. Once it's finished, you know the user entered 'exit' so you can print the error message.

#!/usr/bin/python
friends = {'John' : {'phone' : '0401',
        'birthday' : '31 July',
        'address' : 'UK',
        'interests' : ['a', 'b', 'c']},
    'Harry' : {'phone' : '0402',
        'birthday' : '2 August',
        'address' : 'Hungary',
        'interests' : ['d', 'e', 'f']}}

response = ['']
error_message = "Sorry, I don't know about that. Please try again, or type 'exit' to leave the program: "

while response[0] != 'exit':
    response = raw_input("Please enter search criteria, or type 'exit' to exit the program: ").split()
    try:
        print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]])
    except KeyError:
        print error_message
    except IndexError:
        print error_message

print ('Thank you, good bye!')

This code is a start to what you want, but it still has some bugs. See if you can restructure it so the error message isn't printed when the user enters 'exit'.

Top answer
1 of 2
86

There is no do...while loop because there is no nice way to define one that fits in the statement: indented block pattern used by every other Python compound statement. As such proposals to add such syntax have never reached agreement.

Nor is there really any need to have such a construct, not when you can just do:

while True:
    # statement(s)
    if not condition:
        break

and have the exact same effect as a C do { .. } while condition loop.

See PEP 315 -- Enhanced While Loop:

Rejected [...] because no syntax emerged that could compete with the following form:

    while True:
        <setup code>
        if not <condition>:
            break
        <loop body>

A syntax alternative to the one proposed in the PEP was found for a basic do-while loop but it gained little support because the condition was at the top:

    do ... while <cond>:
        <loop body>

or, as Guido van Rossum put it:

Please reject the PEP. More variations along these lines won't make the language more elegant or easier to learn. They'd just save a few hasty folks some typing while making others who have to read/maintain their code wonder what it means.

2 of 2
9

Because everyone is looking at it wrong. You don't want DO ... WHILE you want DO ... UNTIL.

If the intitial condition is true, a WHILE loop is probably what you want. The alternative is not a REPEAT ... WHILE loop, it's a REPEAT ... UNTIL loop. The initial condition starts out false, and then the loop repeats until it's true.

The obvious syntax would be

repeat until (false condition):
  code
  code

But for some reason this flies over everyone's heads.

🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Bootcamp Python Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 41174392 › loop-until-py
python - Loop until (py) - Stack Overflow
amount_of_names = int(input("Input amount of players: ")) names = [] for counter in range(amount_of_names): name = str(input("Input players first name: ")) names.append(name) #print (names)
🌐
Quora
quora.com › Does-Python-have-an-until-loop
Does Python have an until loop? - Quora
Answer (1 of 7): P̲y̲t̲h̲o̲n̲ ̲d̲o̲e̲s̲n̲’̲t̲ ̲h̲a̲v̲e̲ ̲a̲ ̲d̲e̲d̲i̲c̲a̲t̲e̲d̲ ̲`̲u̲nt̲i̲l̲`̲ ̲l̲o̲o̲p̲ ̲l̲i̲k̲e̲ ̲R̲u̲b̲y̲ ̲o̲r̲ ̲s̲o̲m̲e̲ ̲o̲t̲h̲e̲r̲ ̲l̲a̲n̲g̲u̲a̲g̲e̲s̲ ̲,̲ ̲b̲u̲t̲ ̲y̲o̲u̲ ̲c̲a̲n̲ ̲e̲a̲s̲i̲l̲y̲ ̲r̲e̲p̲l̲i̲c̲a̲t̲e̲ ̲t̲h̲e̲ ̲b̲e̲h̲av̲io̲r̲ ̲u̲si̲n̲g̲ ̲a̲ ̲`̲w̲h̲i̲l̲e̲`̲ ̲l̲o̲o̲...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - In Python, we can simulate the ... condition is met. Do while loop is a type of control looping statement that can run any statement until the condition statement becomes false specified in the loop....
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - If the number the user submits is negative, the loop will keep on running. If it is positive, it will stop. Python does not have built-in functionality to explicitly create a do while loop like other languages.
🌐
Bbolker
bbolker.github.io › math1mp › notes › random › r7_while.html
do-while loops
February 4, 2015 - Googling “repeat vs while” gives a variety of interesting answers (including a Wikipedia article on “do while” (which agrees with the Stack Overflow answer that while True: ... if (condition): break is the way to do this in Python). Some computer languages have specific syntax that let you choose whether the condition is checked at the beginning or end of the loop. Beyond this, there is a question about whether it is more natural to use a “repeat until” construction (the loop repeats until the condition is false) or a “repeat while” construction (the loop repeats while the condition is true).
🌐
Medium
medium.com › @BetterEverything › until-loops-and-do-while-loops-in-python-this-is-how-bfe43b22e6b4
Until Loops and Do While Loops in Python? This is how! | by Better Everything | Medium
August 13, 2024 - To make an Until loop we need to loop until a certain condition evaluates to True. To do that, we can choose between a for loop or a while loop to start repeating lines of code. In the loop we put an if-statement to check a condition.
🌐
Codecademy
codecademy.com › forum_questions › 50f596b8981f64809b000105
I am confused, please explain until loops to me. | Codecademy
First I’ll give you the code, and then I’ll break it down. counter = 1 until counter == 11 puts counter counter = counter + 1 end · this is your counter. the name “counter” is arbitrary. You could call it “bacon.” However, a logical name like “counter” makes your code more readable. This is telling the computer that until the counter gets to 11, it should execute the instructions that follow. If the counter never gets to 11, you’ll have an infinite loop.
🌐
Stack Overflow
stackoverflow.com › questions › tagged › while-loop
Newest 'while-loop' Questions - Stack Overflow
If the condition is met then I want the inner loop to terminate and proceed to the next iteration of the ...
🌐
DataCamp
datacamp.com › tutorial › do-while-loop-python
Emulating a Do-While Loop in Python: A Comprehensive Guide for Beginners | DataCamp
January 31, 2024 - To emulate a "do-while" loop in Python, you can use a while loop with a True condition and strategically place a break statement. Here's an example: while True: # Execute your code here if not condition: break · In this structure, the loop executes at least once.