Python - problem with if/elif and use of default table value

I have a mysql table with one of the columns  ALPAID_ytd  acting as a switch. The default value for the column is 0 (zero) and this represents the off position. The column is changed to a value of 1 when the switch is on. This switch is programmed on/of as the user deploys the application. Below is an image of the mysql panel showing the setup.

While I have no problems switching on/off and updating the table, I have a major probLem in using the values in a set of if/elif filters which  feed a table with purchase information. For example

if ALPAID_ytd != 0 and status == 1:
            print ("elif ALPAID_ytd != 0 and status == 1: fired")
        elif ALPAID_ytd != 1 and status == 1:
            print("elif ALPAID_ytd != 1 and status == 1: fired")

Open in new window

In the above, !=0 gets me the value 1. No problem
However, != 1 does not resolve to 'NOT EQUAL 1' (zero)  but rather  to !=0  which is 1.  ==0 resolves exactly the same way.
Consequently it is not possible to run the filters as I cannot catch the value 0.

mysql panel showing default setting

Here is an output example showing the problem.  Note that the actual value of ALPAID_ytd is 0, but 1 gets inserted into the  if/elif code.

output example from attached code page
Finally, here is the code from my testing page.  I have set the value of ALPAID_ytd to 0 (the problem value). The variable "status"  is a descriptor for the order (mylist). There are three possibilities  which are defined by the mylist sublists

2 ........  eg   mylist = ['1008', [4], [4], [4]]
0.........  eg   mylist = ['1008', [4], [29], [4]]
1   mylist = ['1008', [29], [29], [29]]

You could plug these into line 88 of the code to use different values of "status" - although I do not believe this is connected to the problem

Hopefully you can spot a solution (excluding changing the default value of  ALPAID_ytd to 0  !!)


import collections
import cgi,cgitb
import mysql.connector as conn
def htmlTop():
        <!DOCTYPE html>
        <html lang="en">
                <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
                <title> My Server-side template</title>

def htmlTail():
    print(""" its done</body>

def connectDB():
    db=conn.connect(host='localhost' ,user='root' ,passwd='844cheminduplan' ,db='office')
    cursor = db.cursor()
    return db, cursor 

def activityinfo(db,cursor,courseNumber):
    cursor.execute("""select courseNumber,Activity, tariffstwelve, tariffsthirtysix from activities WHERE courseNumber = "%s" """
    activitydata = cursor.fetchall()
    return activitydata

def SelectALPAID_ytd(db,cursor):
    cursor.execute("""select ALPAID_ytd from registrations WHERE memberID = "%s" """
    return ALPAID_ytd

def OrderProfiler(mylist,db,cursor):
    # provides a list of 1s nd 0eros to describe the list in terms of paid and activity libre activities
    OrderProfile = []
    #del mylist[0] # chopping off the memberID
    mylist = mylist[1]  +  mylist[2]  + mylist[3]
    print(mylist, "this is mylist")
    for each in mylist:
        courseNumber = each
        activitydata =  activityinfo(db,cursor,courseNumber)
        #print(activitydata, "this is activity data")
        #Note activitydata[0][2] is price for 12 trim and activitydata[0][3] is for 36 trim
        if activitydata[0][2] == 0 and activitydata[0][3] == 0:
            OrderProfile.append(0) #activityLibre
        elif activitydata[0][2] >0 and activitydata[0][3] > 0:
            OrderProfile.append(1) # paid
    return OrderProfile
def StatusFinder (OrderProfile):
    # declares an order as being mixed (0), activity libre only(1) or paid only(2) items
    # uses OrderProfile list from OrderProfiler(mylist, db, cursoro) above
    # this status along with the ALPAID_YTD flag  in registrations table determines if DELTA ORDER should be
    # charged the activity libre registration fee, or should be reimboursed the activity libre registration fee
    print (collections.Counter(OrderProfile))
    OrderProfileCount = (collections.Counter(OrderProfile))
    if OrderProfile.count(1) >0 and OrderProfile.count(0) >0:
        print(OrderProfile, "OrderProfile")
        status = 0 #mixed
    elif OrderProfile.count(1) >0 and OrderProfile.count(0)== 0:
        status = 2 #paid_only
    elif OrderProfile.count(1) ==0 and OrderProfile.count(0)>0:
        status = 1 #free_only
    return status

#main program
if __name__== "__main__":
        mylist = ['1008', [29], [29], [29]]
        OrderProfile = OrderProfiler(mylist,db,cursor)
        status = StatusFinder (OrderProfile)
        print(status, "this is order status")
        memberID = mylist[0]
        print (memberID, "this is memberID")
        ALPAID_ytd = SelectALPAID_ytd(db,cursor)
        ALPAID_ytd = ALPAID_ytd [0][0]
        print(ALPAID_ytd, "this is ALPAID_ytd - the on/off switch. Default in table is 0(off) and 1(on)")
        if ALPAID_ytd != 0 and status == 1:
            print ("elif ALPAID_ytd != 0 and status == 1: fired")
        elif ALPAID_ytd != 1 and status == 1:
            print("elif ALPAID_ytd != 1 and status == 1: fired")
        elif ALPAID_ytd != 0 and status == 2:
            print ("elif ALPAID_ytd != 0 and status == 2: fired")        
        elif ALPAID_ytd != 1 and status == 2:
            print ("elif ALPAID_ytd != 1 and status == 2:fired")
        elif ALPAID_ytd != 0 and status == 0:
            print ("elif ALPAID_ytd != 0 and status == 0: fired")        

        elif ALPAID_ytd != 1 and status == 0:
            print ("elif ALPAID_ytd != 1 and status == 0:fired")        
            print("no result")

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BradyPrincipal Data EngineerCommented:
The first issue I see is that table setup for that field. If you set it up as a varchar you should be single quoting the values because it is expecting a string and you are putting in integers into your query. This filed type should be changed to tinyint with a default of 0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The table stores the values as strings and as such you get in `ALPAID_ytd` the strings '0' and '1' - not the numbers 0 and 1 your if's are checking for. As such, your != comparisons will be always false
>>> '0'!=0, '0'!=1, '1'!=0, '1'!=1
(True, True, True, True)

Open in new window

In your code as-is, just change to check for '0' and '1' strings. Long term, maybe check for equality rather than inequality (that might have helped you to spot the problem earlier. maybe)
jameskaneAuthor Commented:
Mark and Nas,


Many thanks to you both for the help. and insight.

Guess I need to swot up some on mysql server !!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.