Python Forum
Problem with and if() logic - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: Problem with and if() logic (/Thread-Problem-with-and-if-logic)



Problem with and if() logic - nikos - Feb-11-2019

		if name and month and year:
			cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) )
			print( 1 )

		elif name and year:
			cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) )
			print( 2 )

		elif month and year:
			cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) ) 
			print( 3 )

		elif year:
			cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year )
			print( 4 )
Its almost correct except the fast that the last "elif" never gets executed. Even if 'month=0', which mean no selection of month still this clause 'elif month and year:' always executes instead of the last one.

Please can someone correct this and explain why?
Thank you!


RE: Problem with and if() logic - nilamo - Feb-11-2019

name = False
month = 0
year = 2019

if name and month and year:
    #cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) )
    print( 1 )
 
elif name and year:
    #cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) )
    print( 2 )
 
elif month and year:
    #cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) ) 
    print( 3 )
 
elif year:
    #cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year )
    print( 4 )
Output:
> python eggs.py 4
I don't get the same output as you, so I'd suggest making sure that your variables actually contain what you think they do.


RE: Problem with and if() logic - nikos - Feb-11-2019

		if name and month != '0' and year != '0':
			cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) )

		elif name and year != '0':
			cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) )

		elif month != '0' and year != '0':
			cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) )

		elif year:
			cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year )
Actually when they contained '0', which is a string which is soemthing and its different from 0 which is like sayif if month is not None.