gpt4 book ai didi

python - bool 的奇怪逻辑

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:45 25 4
gpt4 key购买 nike

我无法理解 python 中的逻辑。这是代码:

maxCounter = 1500
localCounter = 0

while True:
print str(localCounter) + ' >= ' + str(maxCounter)
print localCounter >= maxCounter

if localCounter >= maxCounter:
break

localCounter += 30

结果输出:

...
1440 >= 1500
False
1470 >= 1500
False
1500 >= 1500
False
1530 >= 1500
False
1560 >= 1500
False
...

我在那里有无限循环。为什么?


topPos = someClass.get_element_pos('element')
scrolledHeight = 0

while True:
print str(scrolledHeight) + ' >= ' + str(topPos)
print scrolledHeight >= topPos
if scrolledHeight >= topPos:
print 'break'
break

someClass.run_javascript("window.scrollBy(0, 30)")
scrolledHeight += 30
print scrolledHeight

time.sleep(0.1)

最佳答案

要修复您的代码,请尝试以下操作:

topPos = int(someClass.get_element_pos('element'))

为什么?

当我复制并粘贴您的原始代码时,我得到了这个:

...
1440 >= 1500
False
1470 >= 1500
False
1500 >= 1500
True

我可以找到对您的代码进行的一个小改动,可以重现您所看到的行为,将第一行更改为:

maxCounter = '1500'  # string instead of integer

进行此更改后,我还可以看到您获得的输出:

1410 >= 1500
False
1440 >= 1500
False
1470 >= 1500
False
1500 >= 1500
False
1530 >= 1500
False
etc..

关于python - bool 的奇怪逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3270605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com