2

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)
3
  • How do you define remains same? like it should match previous iteration value or any value that has taken place yet? Please add some more details to the questions Commented Oct 15, 2015 at 10:22
  • You can simply save it into a buffer which will store the value and refresh this buffer on each value change too Commented Oct 15, 2015 at 10:23
  • Yes remains the same between more iterations. The only method is save in a buffer? Commented Oct 15, 2015 at 10:24

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain how old_value, value = value, way_to_new_value works please?
Well, the way_to_new_value stand for your operation that change the value. The assignment is short for assigning on multi lines.
Wow it is what I thought then, is impressive this double assignment.
2
previous = None
current = object()
while previous != current:
    previous = current
    current = ...

Comments

1

A more portable solution would be to make this a class so that an instance holds on to the previous value. I also had a need for a fuzzy match so I included that in the below example.

class SamenessObserver:
    """An object for watching a series of values to see if they stay the same.
    If a fuzy match is required maxDeviation may be set to some tolerance.

    >>> myobserver = SamenessObserver(10)
    >>> myobserver.check(9)
    False
    >>> myobserver.check(9)
    True
    >>> myobserver.check(9)
    True
    >>> myobserver.check(10)
    False
    >>> myobserver.check(10)
    True
    >>> myobserver.check(11)
    False
    >>> myobserver = SamenessObserver(10, 1)
    >>> myobserver.check(11)
    True
    >>> myobserver.check(11)
    True
    >>> myobserver.check(10)
    True
    >>> myobserver.check(12)
    False
    >>> myobserver.check(11)
    True
    >>> 

    """

    def __init__(self, initialValue, maxDeviation=0):
        self.current = 0
        self.previous = initialValue
        self.maxDeviation = maxDeviation

    def check(self, value):
        self.current = value
        sameness = (self.previous - self.maxDeviation) <= self.current <= (self.previous + self.maxDeviation)
        self.previous = self.current
        return sameness

Comments

0

You can:

value_old = 0
value_new = 1
value = [value_old, value_new]
while True:
    # change

    # test
    if value[0] == value[1]:
        break
    else:
        value = [value[1], value[0]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.