2

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()
0

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Ouch - you wouldn't believe that, as a self taught beginner, the indentation thing (coming from VB.net) was something I just didn't know - thank you! Sorted!
@Compoot: the for loop iterates over a range(), which gives you integers starting at 0 going up. It'll thus go through all of fields[0] through to fields[len(fields) - 1], e.g. all the fields.
Martijn - thanks so much - HUGELY! The only thing, as mentioned, is that it now works for all possible entries (wrong entries, and right entries) but a blank entry in username and password also produces a 'access granted' - can't think why!
@Compoot: Do you have any empty strings in your fields list perhaps? Because then it'll find those and thus an empty pair is a valid option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.