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
#2
I'm guessing that is in a loop, might try
if PointFound == True:
    print('stuff here')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
I tried all that type of possible solutions without any luck.

It seems that when the logic is printing the lines 'section... --> False' (or True), each iteration of 'a' is treated independently according to the value of 'a'. However, as soon as I start putting a print instruction (or any other instruction for that matter) into the 'if condition', it seems that all 'PointFound' boolean values for all 7 sections are regarded as a list. In that case, since one of the seven booleans is True, all 7 lines get evalutaed as True.

Notice also that when using the 'if condition', the additional print instruction is already printed out for sections 1 and 2 PRIOR (!) to arriving at section 3, which is the first (and only) section that returns a value True for 'PointFound'

In other words : although looping through 'a' from 1 to 7, the 7 results are not returned as individual values but rather as a list that evaluates entirely to TRUE because one single value is True.

The strange part is why 'True' is only printed one time (for section 3) by the original print instruction while the additional print line is printed 7 times for each section.
Reply
#4
If the return is a list, did you try if PointFound in list: print(stuff).

Not seeing code where the loop starts does not help any.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Next is the entire code block :

# region Step_4

    a = 1
    while a < k + 1:

        # step 4 : check if point is within an area of this province
        #             loop through distinct regions one-by-one

        cursor = con.cursor()

        query = '''SELECT ST_WITHIN(
                        (SELECT ST_SetSRID("GeomPOINT", 4326) FROM public."Urbanisme_POINT"
                            WHERE "Id" = 1),
                        (SELECT ST_SetSRID("geom", 4326) FROM public."province" 
                            WHERE "gid" = ''' + str(a) + ''')
                        )'''

        cursor.execute(query)

        PointFound = cursor.fetchone()

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

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

        print(*PointFound, sep="\n")			# new 1

        a += 1

        if a > k:
            break

        print(*PointFound, sep="\n")			# new 2
        print(PointFound)				# new 3

        # endregion
I have added some 3 more additional print instructions aimed at detecting list issues. I now get the following result :

region 1 --> (False,)
Place my UPDATE sql query here
False
False
(False,)
region 2 --> (False,)
Place my UPDATE sql query here
False
False
(False,)
region 3 --> (True,)
Place my UPDATE sql query here
True
True
(True,)
region 4 --> (False,)
Place my UPDATE sql query here
False
False
(False,)
region 5 --> (False,)
Place my UPDATE sql query here
False
False
(False,)
region 6 --> (False,)
Place my UPDATE sql query here
False
False
(False,)
region 7 --> (False,)
Place my UPDATE sql query here
False
Reply
#6
What is k?
This will take some one with a little more experience than what I have.
My only suggestion would be to try
if PointFound_17 in PointFound:
    print('database query here')
Sorry I could not be any help.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
see my first post : k = maximum numbers of regions (retrieved from database by means of step 3 (= earlier process preliminary to the code I have posted so far)

in other words : iteration of 'a' stops when a > k (see break at the end)

please ignore 'PointFound_17' in last post and consider it to be just 'PointFound'
Reply
#8
One last suggestion add else to your if statement. tell it what to do if it's false.
if PointFound:
    print('stuff')
else:
    pass
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#9
Thanks, but I tried all that already (see my first post).

I am currently trying to store the 7 'PointFound' values for each region into a numpy array for further sorting.
Maybe that will work.
Reply
#10
PointFound is not True or False but rather (True,) or (False,). An empty tuple is False, but a tuple that contains False is True (a non-empty tuple). Since PointFound is either (True,) or (False,), both non-empty tuples, if PointFound always evaluates to True.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ConfigParser(dict_type=) not behaving as expected malonn 5 3,297 Sep-08-2022, 08:42 AM
Last Post: malonn
  Two loops behaving differently DavidTheGrockle 5 2,533 Dec-27-2020, 03:56 AM
Last Post: deanhystad
  else condition not called when if condition is false Sandz1286 10 5,870 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,942 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Requests not behaving... PythonLamer 1 4,804 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