Unable to compare input variable to data within a .txt / Unable to read file to compare data

烈酒焚心 提交于 2020-07-22 05:14:11

问题


Currently I'm in the process of making a program where you can both login and register and the username/password are stored in a separate .txt file. Registering is working fine, the username and password is written without issue but I am having issues with reading from the file in both userRegister and userLogin

The .txt file is formatted by username,password and I was wondering how I would go about reading from the file with the intention of comparing loginUsername and loginPassword to username_password and comparing registerUsername to existing usernames to ensure there are no duplicates.

username_password = open("savedCredentials.txt", "r+")

option = ()
def startMenu():
    option = input("Do you want to [login] or [register] an account?:")
    if option == 'login':
        return userLogin()
    elif option == 'register':
        return userRegister()
    else:
        print("Invalid input, enter either [login] or [register]")
        return startMenu()

def userRegister():
    registerUsername = input("Enter a username: ")
    if registerUsername in username_password:
        print("This username is already in use")
        userRegister()
    else:
        registerPassword = input ("Enter a password: ")
        if len(registerPassword) < 5:
            print("Your password needs to contain 5 or more characters")
            registerPassword()
        elif " " in registerPassword:
            print("Your password cannot contain spaces")
        else:
            register = open("savedCredentials.txt", "a")
            register.write(registerUsername)
            register.write(",")
            register.write(registerPassword)
            register.write("\n")
            print("Your username and password have been successfully registered")

def userLogin():
    loginUsername = input("Enter your username: ")
    if loginUsername in username_password:
        loginPassword = input("Enter your password: ")
        if loginPassword in username_password:
            successfulLogin()
    else:
        print("This username isn't registered to an account, please try again")
        return userLogin()

def successfulLogin():
    print("You have been logged in")

username_password.close()


回答1:


A few things:

  1. You aren't calling any functions in your above code, so nothing will run as it stands.

  2. You cannot iterate a Text wrapper, you can get around this by just reading your file by .read()

  3. If you close() the file outside of your functions, you'll get an error that the file is closed, close the file within your function instead (whenever the user is done).

  4. It appears once you go through the conditional if search, the .read() doesn't work anymore for the 2nd round. Don't quite know why (maybe someone else here can go into more detail), but the workaround is instead convert your file to a list, and search through that instead.

The below works, but it is a bit ugly (I have to go know, but wanted to post this real quick so you have at least a working example, and can build off of it).

username_password2=[]
with open("savedCredentials.txt", "r+") as file:
    for lines in file:
        a=lines.replace(',',' ')
        b=a.split()
        username_password2.append(b)

username_password = [x for y in username_password2 for x in y]

option = ()
def startMenu():
    option = input("Do you want to [login] or [register] an account?:")
    if option == 'login':
        return userLogin()
    elif option == 'register':
        return userRegister()
    else:
        print("Invalid input, enter either [login] or [register]")
        return startMenu()

def userRegister():
    registerUsername = input("Enter a username: ")
    if registerUsername in username_password:
        print("This username is already in use")
        userRegister()
    else:
        while True:
            registerPassword = input ("Enter a password: ")
            if len(registerPassword) < 5:
                print("Your password needs to contain 5 or more characters")
            elif " " in registerPassword:
                print("Your password cannot contain spaces")
            else:
                register = open("savedCredentials.txt", "a")
                register.write(registerUsername)
                register.write(",")
                register.write(registerPassword)
                register.write("\n")
                print("Your username and password have been successfully registered")
                register.close()
                break

def userLogin():
    loginUsername = input("Enter your username: ")
    if loginUsername in username_password:
        loginPassword = input("Enter your password: ")
        if loginPassword in username_password:
            successfulLogin()
        else:
            print(username_password)
    else:
        print("This username isn't registered to an account, please try again")
        return userLogin()

def successfulLogin():
    print("You have been logged in")

startMenu()



回答2:


You have to add the read function to your file opening.

replace the line username_password = open("savedCredentials.txt", "r+")

by

username_password = open("savedCredentials.txt", "r+").read()

then you have to remove the line username_password.close()

also, you need to call your startMenu function so add startMenu() at the bottom of your code.



来源:https://stackoverflow.com/questions/62842403/unable-to-compare-input-variable-to-data-within-a-txt-unable-to-read-file-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!