1

I have made a singup window which saved the data of people in text file and in login window i used a for loop to read thorugh the data and check if the password and username is correct or not but everytime i press login, it only shows incorrect username or password and doesnt open the dashboard window even if the password is correct.

def check():
        storefile = open('store.txt','r')
        for line in storefile:
            if namepasss and nameuser in line:
                dashboard()
            else:
                Label(my, text="Incorrect username or\n password.", fg="red", bg="lightgray").grid(row=4, column=2)


    name_useval = StringVar()
    name_passval = StringVar()
    namepasss = name_passval.get()
    nameuser = name_useval.get()
    Entry(my,  textvariable=name_useval,bg="#e3e2e2", bd=0, highlightthickness=0,).grid(row=2, column=2)
    Entry(my,  textvariable=name_passval,bg="#e3e2e2", bd=0, highlightthickness=0,).grid(row=3, column=2)
    login = Button(my, text="Login", font=("sans serif", 12, ), bg="lightgray", bd=2,
                    highlightthickness=0, fg="Black", command=check).grid(row=10, column=2)

this is how the data are saved in a text file after users singup in singup window

('randomguy', 'randomguy@gmail.com', '901909210', 'randompassword')
('randomguy2', 'randomguy2@gmail.com', '901978210', 'randompassword2')
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • `if namepasss and nameuser in line` is *logically* wrong when checking whether `namepasss` and `nameuser` are within `line`. Also you don't provide information on how username and password are stored in the text file. Furthermore, `namepasss` and `nameuser` are empty strings. – acw1668 Jun 28 '21 at 07:35
  • Can you share in which format is the username/password stored in a txt file? Its is separated by a comma or a space? –  Jun 28 '21 at 07:40
  • this is how the data are saved in a text file after users singup in singup window ```('randomguy', 'randomguy@gmail.com', '901909210', 'randompassword') ('randomguy2', 'randomguy2@gmail.com', '901978210', 'randompassword2')``` – Shikhar Adhikari Jun 28 '21 at 08:07

1 Answers1

0

The statement

namepasss and nameuser in line

is actually evaluated as

namepasss and (nameuser in line)

due to operator precedence. Therefore, taking into consideration the laziness of the and operator, the (poorly written) order of evaluation is as follows:

if namepasss == True:
    if (nameuser in line) == True:
        return True
return False

Since namepasss is a string, anything but "" will evaluate to True.

What you really need is the following:

(namepasss in line) and (nameuser in line)

Without knowing what is written in the file and what values are assigned to namepasss and nameuser it's difficult to help more.

Alex Mandelias
  • 436
  • 5
  • 10
  • That is true but in login systems, it can lead to a little bug. Example. if a username is ```abcd``` and then a user entered ```abc```, then that username would be taken because ```abc``` is in ```abcd``` –  Jun 28 '21 at 07:44
  • Yes of course, the answer is based on what the question had as the condition. With no further information about the file's contents there aren't any better solutions to the problem. – Alex Mandelias Jun 28 '21 at 08:01
  • this is how the data are saved in a text file after users singup in singup window ```('randomguy', 'randomguy@gmail.com', '901909210', 'randompassword') ('randomguy2', 'randomguy2@gmail.com', '901978210', 'randompassword2')``` – Shikhar Adhikari Jun 28 '21 at 08:08
  • Consider removing the parentheses and the quotation marks. This way you will have a csv file which is very easily parsed. For each line do `line.split(',')` and check the user credentials against the items at the appropriate indexes in the list, 0 and 3 in this case. If you need to keep the quotation marks then you need to take extra care to remove them when comparing user credentials against them. – Alex Mandelias Jun 28 '21 at 08:15
  • is this how i am supposed to do this?? ```for line in storefile: line.split(',') if (namepasss in line[0:3]) and (nameuser in line[0:3]): dashboard()``` – Shikhar Adhikari Jun 28 '21 at 08:36