Python Login and Register System using text files

僤鯓⒐⒋嵵緔 提交于 2021-01-29 15:20:48

问题


Hey I am trying to create a system using text files where a user can sign up and log in. All the data will be stored in plain text in a text file called User_Data.txt. My code works but I would like to know if there is anything I missed or If I could improve it in any way. Sorry for the Bad code Formatting in advance.

def choices():
    print("Please choose what you would like to do.")
    choice = int(input("For Sigining Up Type 1 and For Signing in Type 2: "))
    if choice == 1:
       return getdetails()
    elif choice == 2:
       return checkdetails()
    else:
       raise TypeError

def getdetails():
    print("Please Provide")
    name = str(input("Name: "))
    password = str(input("Password: "))
    f = open("User_Data.txt",'r')
    info = f.read()
    if name in info:
        return "Name Unavailable. Please Try Again"
    f.close()
    f = open("User_Data.txt",'w')
    info = info + " " +name + " " + password
    f.write(info)

def checkdetails():
    print("Please Provide")
    name = str(input("Name: "))
    password = str(input("Password: "))
    f = open("User_Data.txt",'r')
    info = f.read()
    info = info.split()
    if name in info:
        index = info.index(name) + 1
        usr_password = info[index]
        if usr_password == password:
            return "Welcome Back, " + name
        else:
            return "Password entered is wrong"
    else:
        return "Name not found. Please Sign Up."

print(choices())

回答1:


There is a lot of improvements You could do. First of all, split functionality to smaller function.

PASSWORD_FNAME = "User_Data.txt"

def get_existing_users():
    with open("r", PASSWORD_FNAME ) as fp:
         for line in fp.readlines():
             # This expects each line of a file to be (name, pass) seperated by whitespace
             username, password = line.split()
             yield username, password

def is_authorized(username, password):
    return any((user == (username, password) for user in get_existing_users()) 

def user_exists(username):
    return any((usr_name == username) for usr_name, _ in get_existing_users())
    # above is equivalent of:
    #
    # for usr_name, _ in get_existing_users():
    #     if usr_name == username:
    #        return True
    # return False

def ask_user_credentials():
    print("Please Provide")
    name = str(input("Name: "))
    password = str(input("Password: "))
    return name, password

def checkdetails():
    name, password = ask_user_credentials()
    if is_authorized(name, password):
       return "Welcome Back, " + name
    if user_exists(name):
       return "Password entered is wrong"
    return "Name not found. Please Sign Up."

def getdetails():
    name, password = ask_user_credentials()
    if not user_exists(name):
       return "Name Unavailable. Please Try Again"
    # Not sure tho what would You like to do here

It's always good to remember to always close your file if you read it. So if you do something like: f = open("r", "file.txt") remember to always call f.close() later. If you use context manager and do it like:

with open("r", "file.txt") as fp:
     print(fp.read())

it will automatically close the file for you at the end.




回答2:


Firstly, fix the spelling error at int(input("For Sigining Up Type 1") Other than that I would add some kind of purpose, for example storing secret numbers or something.




回答3:


For example you can extend your script with a simple password recovery system. I think it could be useful to learn...

You can implement a sort of a simple hashing system in order to avoid saving the password as plain text.

If you want to add a GUI, please consider using Tkinter. https://docs.python.org/3/library/tkinter.html

Let we know.

Good Luck and Keep Coding with <3



来源:https://stackoverflow.com/questions/59121573/python-login-and-register-system-using-text-files

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