Search code examples
pythonpasswordsuser-input

How can I see if my password variable is within a line so that my code can proceed?


I am creating a password re-setter for school, and i want to check that if the password is within the first line of the text file, then it can say "Okay, choose a new password")

Newpass=""
Network=""
setpass=""
Newpass=""
password=""
def newpass():
    Network=open("Userandpass.txt")
    lines=Network.readlines()
    password=input("just to confirm it is you, re-enter your old password:")
    for i in range (3):
        if password in line:
             newpass=input("Okay, choose a new password ")
             Network.close()
             Network=open("Userandpass.txt","a")
             if len(newpass)>= 8 and newpass[0].isalnum()==True and newpass[0].isupper()==True:
                print('password change successful')
                Network.write("New Password  : " + newpass )
                Network.close()
                break
             else:
                print("password did not match requirements, try again ")
        else:
            print("error")
            break
    print("3 tries up or else password updated")

Network=open("Userandpass.txt","w")
Network.write(input("What is your Username")+",")
Network.write(input("Password:")+ ",")
question=input("Do you want to change your password?")
if question=="yes":
    Network.close()
    newpass()
else:
     Network.close()
     print("Okay thank you.")

Please help! I have been looking all over here and i can't find a solution


Solution

  • You can try by two things :

    lines=Network.read() # change here
    password=input("just to confirm it is you, re-enter your old password:")
    for i in range (3):
        if password == lines.split(",")[1]: # change here also
    

    Explanation :

    The problem with readlines is o/p as list where read return as string which is better one to use here .
    The second thing it returns as a single string ie, combined form of name and password just with ,. So if you split it you will get an list of separated values. Then you can take only password from it and check with input.
    In your code you are just checking the input element is just present in that whole string, not checking whether it is same password or not