5

I am making a login system for my project, and I have the usernames and passwords stored in a text file, with usernames in the first column and passwords in the second column, and then separating each login/password with a new line and using : as a barrier between the username/password.

Upon entering the correct username and password, I always get incorrect login, however if I only compare the username to the file it functions properly. Even if I print the password and username straight from the file and then print it next to the username/password I entered, it is still the exact same yet still say incorrect login!

def login():
file=open("user.txt","r")
user=input("enter usename")
password=input("enter password") 
Check=False
for line in file:
    correct=line.split(":")
    if user==correct[0] and password==correct[1]:
        Check=True
        break
if Check==True:
    print("succesffuly logged in")
    file.close()
    mainMenu()
else:
    print("incorrect log in")
    file.close()
    login()
Drise
  • 4,310
  • 5
  • 41
  • 66
sean
  • 61
  • 4

2 Answers2

5

I suspect you have a \n at the end of each user / password string. I suspect line looks like user:pass\n after being read in. Use line.strip().split(':') to remove the newline, which is causing password==correct[1] to fail.

Replace:

for line in file:
    correct=line.split(":")

With:

for line in file:
    correct=line.strip().split(":")

For why, see https://docs.python.org/2/library/string.html#string.strip

string.strip(s[, chars])

Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

Community
  • 1
  • 1
Drise
  • 4,310
  • 5
  • 41
  • 66
  • i'm sorry but how do i implement that into the code, i'm not that sure on what you mean – sean Mar 28 '18 at 15:50
  • https://gyazo.com/ed0b547b65eae8d0dee2a7732992a6dc i got this error when trying your code, i am using python 3.5 if that changes anything and no modules – sean Mar 28 '18 at 15:56
  • Use `line.strip().split(":")`, not `line.strip().line.split(":")`. – Drise Mar 28 '18 at 15:58
  • ah yes that worked! thanks a lot for the patience and answers means a lot! – sean Mar 28 '18 at 16:03
  • Keep in mind that `strip` will remove *all* whitespace. If your passwords end with any other whitespace, you will have issues. – Drise Mar 28 '18 at 16:07
2

We can just check using in

def login():
    file = open("user.txt", "r")
    user = input("enter usename ")
    password = input("enter password ")
    if ('{0}:{1}'.format(user, password)) in file:
        print('yay') 
    else:
        print('Boo !! User not found')

login()

if you wanted to use the for loop I would suggest:

def login():
    file = open("user.txt", "r")
    user = input("enter usename ")
    password = input("enter password ")
    for line in file:
        temp_user, temp_password = line.strip().split(':')
        if temp_user == user and temp_password == password.strip():
            print('yay')
        else:
            print('boo username and password not found!')


login()

Really important, WARNING! Please take necessary security measurements as this code does not provide any, there are a lot of vulnerabilities that could be exploited. No hashing function and Python itself does not provide a lot of security, I would suggest using getpass.getpass explanation HERE

innicoder
  • 2,612
  • 3
  • 14
  • 29
  • You can check if a string exists in an entire file using `in`? – Drise Mar 28 '18 at 15:48
  • i still get incorrect result when entering right username and password – sean Mar 28 '18 at 15:49
  • https://gyazo.com/df0f18cd93d9d6a380e8129fe0ff1643 this is the result i get when trying the second methodm still comes wrong for some reason – sean Mar 28 '18 at 15:53
  • Of course, no worries, however, I'll leave the warning but highly suggest for `sean` to research this topic. @sean one sec. – innicoder Mar 28 '18 at 15:56
  • and yes Drise we can use `in` to check an entire string or file. Here's a great description of what `in` does https://stackoverflow.com/a/19775766/6929467 – innicoder Mar 28 '18 at 16:11
  • I was always under the impression that you need to read each line in a `for` loop and check each line individually. – Drise Mar 28 '18 at 16:13
  • Haha, happens to me too, these moments when there's something so simple and you just didn't know... My friend asked me a month ago how was I doing *that* (I was alt-tabbing), he's an IT student and profesionally does Wordpress. Thankfully, we can all learn from each other. – innicoder Mar 28 '18 at 16:15
  • pedantic, but 'user:password in' may return false positives if actual is like "user1:pass1longer" but entered "user:pass1". adding the problem \n to the format might help. – JL Peyret Mar 29 '18 at 13:44
  • I agree, also if the password or username is for instance `password = "user1:password"` I do repeat that this code should not be used in a real code, because it does not offer security and there are many issues and possible exploits. – innicoder Mar 29 '18 at 16:17