我想知道是否有一种优雅的方法来查看是否可以检查while循环中连续变化的值,如果值停止变化并保持不变,则停止while循环。
例如:
Value = 0
while True:
value changes everytime
(if value still the same break)
答案 0 :(得分:4)
这样怎么样?顺便说一句:修复你的拼写错误setValueOfElement = function (element, value, attempts) {
if (attempts == null) {
attempts = 10;
}
return element.sendKeys(value).then(function (found) {
}, function (err) {
if (attempts > 0) {
browser.sleep(100);
return exports.setValueOfElement(element, attempts - 1);
} else {
throw err;
}
});
};
在python中不是while
。
While
答案 1 :(得分:1)
<ul class="nav navbar-nav">
<?php foreach ($categories as $category) { ?>
<?php if ($category['children']) { ?>
<li class="dropdown"><a href="<?php echo $category['href']; ?>" class="dropdown-toggle" data-toggle="dropdown"><?php echo $category['name']; ?></a>
<div class="dropdown-menu">
<div class="dropdown-inner">
<?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
<ul class="list-unstyled">
<?php foreach ($children as $child) { ?>
<li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li>
<?php } ?>
</ul>
<?php } ?>
</div>
<a href="<?php echo $category['href']; ?>" class="see-all"><?php echo $text_all; ?> <?php echo $category['name']; ?></a> </div>
</li>
<?php } else { ?>
<li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
<?php } ?>
<?php } ?>
</ul>
答案 2 :(得分:0)
你可以:
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]]
答案 3 :(得分:0)
更可移植的解决方案是将其设为一个类,以便实例保留先前的值。我也需要模糊匹配,因此我将其包含在以下示例中。
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