0

I have a program that registers an account and I am now working on a function to log said account in. I have saved the lists "usernames" and "passwords" to separate text files.

usernames = []
passwords = []
usernames.append(username)
passwords.append(password)

usernamesFile = open("usernames.txt","a")
for usernames in usernames:
    usernamesFile.write("%s\n" % usernames)
usernamesFile.close()

passwordsFile = open("passwords.txt", "a")
for passwords in passwords:
    passwordsFile.write("%s\n" % passwords)
passwordsFile.close()

while True:
    loginUsername = raw_input("Enter your username: ")
    usernamesFile = open("usernames.txt", "r")
    lines = usernamesFile.readlines() 
    if loginUsername in usernames:
        index = usernames.index(username)
        break
    else:
        print "Username not found"
        continue

My problem is, even though I have tested the text file and it has saved all of the usernames I have registered, the search still comes back "Username not found". I am not an expert in Python so if you could please explain in a simple way that would be great!

2
  • 1
    It doesn't look like your code ever does anything with lines, which I'm guessing is what you wanted to search through? (also: as a side note: even if you're just learning python, you should immediately purge the idea of ever storing a list of passwords in the clear). Commented Dec 28, 2017 at 12:25
  • How do I not store the list of passwords in the clear? Commented Dec 28, 2017 at 13:48

2 Answers 2

1

You have not used lines read from usernames.txt file in your code for searching the username in it. So, modify your code as:

usernames = []
passwords = []
usernames.append(username)
passwords.append(password)

usernamesFile = open("usernames.txt","a")
for usernames in usernames:
    usernamesFile.write("%s\n" % usernames)
usernamesFile.close()

passwordsFile = open("passwords.txt", "a")
for passwords in passwords:
    passwordsFile.write("%s\n" % passwords)
passwordsFile.close()

while True:
    loginUsername = raw_input("Enter your username: ")
    usernamesFile = open("usernames.txt", "r")
    lines = usernamesFile.readlines() 
    if loginUsername in lines:
        index = lines.index(username)
        break
    else:
        print "Username not found"
        continue
Sign up to request clarification or add additional context in comments.

Comments

0

It is because you are writing this way

usernamesFile.write("%s\n" % usernames)

adding "\n" to the end of each username so your condition

if loginUsername in usernames:

is always False unless you remove the "\n" or you add it with loginUsername

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.