Reputation: 5649
I have this text file that contains:
Ryan#Male#Ryan123#Ryan321#
Lina#Female#Lina123#Lina321#
the order is Name#Gender#username#password
.
user = username.get() //from tkinter entry
pass = password.get()
counter = IntVar()
counter = 0
file = open("user.txt", "r")
for login in file:
login = login .split('#')
if user == login [2]:
counter += 1
if pass== login [3]:
counter += 1
file.close()
if counter == 2:
//go to next page
else:
print "wrong username or password!"
this works, but, when I tried to print login[2], it returned;
Ryan123
Lina123
and when I used Ryan
as username, I still can login using Lina
's password aswell. How can I make it check the content in .txt file row per row?
I mean like, check this row first:
Ryan#Male#Ryan123#Ryan321#
and when the login info is not found, it will proceed to next row.
Upvotes: 1
Views: 650
Reputation: 11596
You are checking two different fields possibly lying on two different lines (e.g. the user matches Ryan and the password matches Lina's).
Normally user and password must both match for a single given user.
So you can get rid of the counters and try with
pwd = password.get()
with open("user.txt", "r") as file:
for login in file:
login = login .split('#')
if user == login[2] and pwd == login[3]:
pass # go to next page, substitute pass on this line!
else:
print "wrong username and password!"
Upvotes: 1
Reputation: 98856
The password problem is because you never reset counter between lines. But it can be done even more simply without counting conditions:
file = open("user.txt", "r")
for login in file:
login = login.split('#')
if user == login[2] and password == login[3]:
print 'Correct username/password'
break
else:
print 'Invalid username/password'
pass
, by the way, is a keyword in Python, so it's a good idea to use something else for your variable name.
Upvotes: 2
Reputation: 2581
You need an "and" condition here, since password and username should match!
he_is_cool = False
for login in file:
login = login .split('#')
if user == login [2] and pass== login [3]:
he_is_cool = True
break
file.close()
if he_is_cool:
//go to next page
else:
print "wrong username or password!"
Note that break
will "kill" your loop, when you have identified the user. The program will also work without break
.
Upvotes: 1