Home > OS >  Python IF statements not running despite conditions met
Python IF statements not running despite conditions met

Time:03-03

In my code I have grabbed an integer from a text file and globally declared it as the variable 'accessLevel1', so it could be used to run validation checks in other areas of the code. However, despite the variable value being saved as '2' when using the first staff login details, an IF statement dependent on the variable being '2' is not running at all.

The contents of the text file titled 'staffDetails':

Jonas,Hills,JHills375,Sweirl900,2,
Ellie,Glover,EGlover919,KHaisen10,1,

Code used for grabbing the variable:

validateStaff = open("staffDetails.txt", "r")
    for row in validateStaff:\
        record = row.split(",")
        if record[2] == (entered_text):
            forename = record[0]
            surname = record[1]
            username = record[2]
            password = record[3]
            accessLevel = record[4]
            if record[3] == (entered_text2):
                global forename1
                global accessLevel1
                global surname1
                surname1 = surname
                forename1 = forename
                accessLevel1 = accessLevel

The problem code

def modclick(): #A function that occurs when a button is pressed
    print("Your access level is: "   (accessLevel1)) #This is here for demonstrative purposes of the problem
    if accessLevel1 == '1':
        errorLabelstock.config(fg='red') #this actually works somehow
        
    if accessLevel1 == '2':
        modifystock() #This function isn't called despite printing that the access level is 2

    if accessLevel1 == '3':
        modifystock()

Proof that the system interpretted the variable 'accessLevel1' to be of the value 2 yet not executing the IF statement: enter image description here

CodePudding user response:

Try adding the following line, this will fix it

accessLevel1 = str(accessLevel1).strip()
  • Related