PjMpire
PjMpire

Reputation: 15

Python enumerating - endless loop on list

I have a login program that checks a list for passwords and usernames. If credentials are found, a dialog window pops-up saying "Success", otherwise it shows "invalid credentials".

However, I am having a problem where for each line it's displaying a dialog window instead of enumerating through the list and then displaying the correct prompt. In short my program is displaying one of the above mentioned prompts for each line.

I'm using PyQt4 for the GUI, here's my code:

def process_login(self, username, password):
    loggedin = False
    file = open('acclist.txt')
    login_info = [line.strip().split() for line in file]
    while not loggedin:
        for pos, line in enumerate(login_info):
            if username == line[0] and password == line[1]:
                QtGui.QMessageBox.information(self, 'Login', 'Login Successful')
                loggedin = True
            if loggedin is not True:
                QtGui.QMessageBox.warning(self, 'Warning!', 'Incorrect credentials')

Any ideas?

Upvotes: 0

Views: 178

Answers (3)

Omar Einea
Omar Einea

Reputation: 2524

You don't need a while loop really. You could loop through the file lines, once credentials match you break, otherwise if loop ends with no match, then login failed. like so:

def process_login(self, username, password):
    loggedin = False
    file = open('acclist.txt')
    login_info = [line.strip().split() for line in file]
    file.close()

    for line in login_info:
        if username == line[0] and password == line[1]:
            QtGui.QMessageBox.information(self, 'Login', 'Login Successful')
            loggedin = True
            break

    if not loggedin:
        QtGui.QMessageBox.warning(self, 'Warning!', 'Incorrect credentials')

Upvotes: 2

shahaf
shahaf

Reputation: 4983

def process_login(self, username, password):
    file = open('acclist.txt')
    login_info = [line.strip().split() for line in file]
    while True:
        for pos, line in enumerate(login_info):
            if username == line[0] and password == line[1]:
                QtGui.QMessageBox.information(self, 'Login', 'Login Successful')
                break

        QtGui.QMessageBox.warning(self, 'Warning!', 'Incorrect credentials')
        break

Note: the while loop is not important it's used as a context for login success/failed instead of saving outside scope variable

don't forget to close the file, a better implementation will be like so

def process_login(self, username, password):
    logged_in = False
    with open('acclist.txt', 'r') as read_file:
        for line in read_file:
            if username == line[0] and password == line[1]:
                QtGui.QMessageBox.information(self, 'Login', 'Login Successful')
                logged_in=True
                break
        if not logged_in:
            QtGui.QMessageBox.warning(self, 'Warning!', 'Incorrect credentials')

Upvotes: 0

Jakob Sachs
Jakob Sachs

Reputation: 715

The Problem is that your Dialog is inside the for-loop. Means it is being opened for every element. A way to fix this would be something like:

def process_login(self, username, password):
  loggedin = False
  file = open('Accounts.txt')
  login_info = [line.strip().split() for line in file]
  while not loggedin:
      incorrect = False
      for pos, line in enumerate(login_info):
          if username == line[0] and password == line[1]:
              loggedin = True
          if loggedin is not True:
              incorrect = True
      if incorrect:
           QtGui.QMessageBox.warning(self, 'Warning!', 'Incorrect credentials'
      else:
           QtGui.QMessageBox.information(self, 'Login', 'Login   Successful')

Upvotes: 0

Related Questions