Strange logic with bool

I cannot understand one thing with logic in python. Here is the code:

maxCounter = 1500
localCounter = 0

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

   if localCounter >= maxCounter:
      break

   localCounter += 30

And the result:

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

And I have an infinite loop there. Why?


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)
+3
source share
2 answers

To fix your code, try the following:

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

Why?

When I copy and paste the source code, I get the following:

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

A small change that I can find for your code that reproduces the behavior you see is to change the first line to this:

maxCounter = '1500'  # string instead of integer

After making this change, I can also see the result you get:

1410 >= 1500
False
1440 >= 1500
False
1470 >= 1500
False
1500 >= 1500
False
1530 >= 1500
False
etc..
+4
source

The problem seems to be on this line:

topPos = someClass.get_element_pos('element')

, topPos, . , .

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

, , CPython v2.7 int .

+1

Source: https://habr.com/ru/post/1755091/


All Articles