2

i have a text file with following data: deric, robert, mathew,

Now, i want the user to input a string and then compare that string with the text file..

 fo = open("d:\\ar.txt", "r")
 file_contents = fo.read()
 print('Enter your password.')
 typedPassword = input()
 for i in file_contents:
     if typedPassword == file_contents:
         print('Access granted')
     else:
         print('Access denied')

However, this doesn't seem to work. any thoughts?

2

2 Answers 2

3

Try this:-

fo = open("poopy.txt", "r")
file_contents = fo.read()
print('Enter your password.')
typedPassword = input()
Flag = 0
for i in file_contents.split('\n'):
    if typedPassword == i:
        Flag = 1
if Flag == 1:
    print('Access Granted')
else:
    print('Access Denied')
Sign up to request clarification or add additional context in comments.

Comments

0

You were checking if the inputted password maches the whole file... This checks each line

username = input("Input your username")
password = input("Input your password")
correct = None
with open("d:\\ar.txt", "r") as file:
    for line in file:
        line = line.split(",")
        if username == line[0] and password == line[1]:
            correct = True
if correct:
    print("") # placeholder
    #(logged in code)

for this to work your file should be of the fornat

username,password,

username,password,

username,password,

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.