简体   繁体   中英

Python - if and else inside for loop with count not working

This is a simple login program - the logic works in VB.Net but I cannot translate it into Python. The Python code does NOT work, however the VB.Net code provided by two users (un_lucky and Jerry) on Stackoverflow does. Can anyone point out a fix for the python code for me please? It basically produces a series of "Access Denied" instead of just one, when the password is incorrect, and if the username or password is the SECOND in the array, then it produces one Access Denied followed by an Access Granted.

count=0
for counter in range(0,len(fields)):
    if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]:
        count=count+1

        if count > 0:
            welcome=Label(myGui,text="Access Granted. Loading Profile ....")
            welcome.pack()
    else:
                denied=Label(myGui,text="Access Denied")
                denied.pack()

and here is the VB.net code (logic) that does work to achieve pretty much the same thing except that the python program above is reading from a text file.

It now works perfectly ( nearly ) but a blank entry in username and password also produces "Access Granted"...can't figure out why! Whole code below:

def verifylogin():

fin=open("moosebook.txt","r")
data=fin.readlines()
for line in data:
    fields=line.split()
    fields=[i.rstrip("','") for i in fields] #strips the named character from END of field
    fields=[i.replace("'",'') for i in fields]#when reading the list, you want to remoe the ',' so it isn't part of the username or password
    fields=[i.replace("(",'') for i in fields] #simiarly, remove the bracket and replace it
    fields=[i.replace(")",'') for i in fields] #simiarly, remove the bracket and replace it

    line=line.rstrip()
    print(fields)

flag=0
for counter in range(0,len(fields)):
    if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]:
        flag=flag+1

if flag>0:
            welcome=Label(myGui,text="Access Granted. Loading Profile ....")
            welcome.pack()
else:
                denied=Label(myGui,text="Access Denied")
                denied.pack()

Indentation is important in Python. You put your if test inside the loop . You need to remove the extra indentation to place it outside the loop:

for counter in range(0,len(fields)):
    if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]:
        count=count+1

if count > 0:
    welcome=Label(myGui,text="Access Granted. Loading Profile ....")
    welcome.pack()
else:
    denied=Label(myGui,text="Access Denied")
    denied.pack()

Note how the if and else lines now start at the same column as the for line.

Because in your code the if statement was indented to a deeper level (the same as the if test checking for the password) you created the labels multiple times, once for each run through the loop.

Indentation works to delimit blocks of code the same way that the VB.NET code uses Next and End If to delimit the loop and the conditional tests explicitly.

The loop can probably be cleaned up; calling textlogin.get() and textpassword.get() just the once, and using zip() to pair up the field values, and any() to see if there is a first match (which is enough here):

login, pw = textlogin.get(), textpassword.get()
if any(pair == (login, pw) for pair in zip(fields, fields[1:])):
    welcome=Label(myGui,text="Access Granted. Loading Profile ....")
    welcome.pack()
else:
    denied=Label(myGui,text="Access Denied")
    denied.pack()

If you meant to loop through the fields as pairs (and not as a sliding window), then use:

login, pw = textlogin.get(), textpassword.get()
fields_iter = iter(fields)
if any(pair == (login, pw) for pair in zip(fields_iter, fields_iter)):
    welcome=Label(myGui,text="Access Granted. Loading Profile ....")
    welcome.pack()
else:
    denied=Label(myGui,text="Access Denied")
    denied.pack()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM