Python Forum
if condition not behaving as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if condition not behaving as expected
#1
I have a while loop in which I check if a certain geographic point is located within any of the 7 regions of a given province (using PostGis ST_WITHIN).

'a' is region number, 'k' is the last region number and 'PointFound' = is a boolean.

cusor.execute(query)

        PointFound = cursor.fetchone()

        print('region ' + str(a) + ' --> ' + str(PointFound))

        a += 1

        if a > k:
            break
This works fine and is giving me the expected output :

region 1 --> (False,)
region 2 --> (False,)
region 3 --> (True,)
region 4 --> (False,)
region 5 --> (False,)
region 6 --> (False,)
region 7 --> (False,)

As shown, the point is located within region number 3.

However, I now want to update the region number into my database, so I changed my code as follows (for testing purposes : later on new print() will have to be replaced by an UPDATE query to database):

cusor.execute(query)

        PointFound = cursor.fetchone()

        print('region ' + str(a) + ' --> ' + str(PointFound))

        if PointFound:
	        print('Place my UPDATE sql query here')

        a += 1

        if a > k:
            break
I was expecting following result :

region 1 --> (False,)
region 2 --> (False,)
region 3 --> (True,)
Place my UPDATE sql query here
region 4 --> (False,)
region 5 --> (False,)
region 6 --> (False,)
region 7 --> (False,)

However, I am getting :

region 1 --> (False,)
Place my UPDATE sql query here
region 2 --> (False,)
Place my UPDATE sql query here
region 3 --> (True,)
Place my UPDATE sql query here
region 4 --> (False,)
Place my UPDATE sql query here
region 5 --> (False,)
Place my UPDATE sql query here
region 6 --> (False,)
Place my UPDATE sql query here
region 7 --> (False,)
Place my UPDATE sql query here

notwithstanding the additional print instruction is placed after 'PointFound' is fetched for every new 'a' BUT before I increment my 'a' value with '1'

Whatever I tried (simple if, if.. elif ..else, while, …. etcetera), nothing seems to solve my problem.

What am I doing wrong here ? Why is my 'if PointFound:' condition not doing what it is expected to do ?
Reply


Messages In This Thread
if condition not behaving as expected - by EricBHK - May-16-2020, 10:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ConfigParser(dict_type=) not behaving as expected malonn 5 3,333 Sep-08-2022, 08:42 AM
Last Post: malonn
  Two loops behaving differently DavidTheGrockle 5 2,558 Dec-27-2020, 03:56 AM
Last Post: deanhystad
  else condition not called when if condition is false Sandz1286 10 5,932 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,162 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Requests not behaving... PythonLamer 1 4,830 Dec-01-2017, 06:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020