pythonpython-2.7loopswhile-loopimperative-programming

Check if a value still remains the same in a While loop Python


I want know if there is a elegant method for looking if a value that continually changes in a while loop can be checked and stop the while loop if the value stops change and remains the same.

For example:

Value = 0
while True:
    value changes everytime
    (if value still the same break)

Solution

  • How about this way? BTW: Fix your typo error while is not While in python.

    value = 0
    while True:
        old_value, value = value, way_to_new_value
        if value == old_value: break