使用文本文件的Python登录和注册系统

问题描述 投票:0回答:3

[嘿,我正在尝试使用文本文件创建系统,用户可以在其中注册并登录。所有数据将以纯文本格式存储在名为User_Data.txt的文本文件中。我的代码可以工作,但是我想知道是否有任何我想念的东西,或者是否可以通过任何方式加以改进。抱歉,错误代码已提前格式化。

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())
python python-3.x authentication text-files registration
3个回答
0
投票

首先,将拼写错误修复为int(input("For Sigining Up Type 1"),除此之外,我会添加某种目的,例如存储秘密号码或其他内容。


0
投票

您可以做很多改进。首先,将功能拆分为较小的功能。

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

记住,阅读文件时总是关闭它总是好事。因此,如果您执行以下操作:f = open("r", "file.txt")记得以后总是打电话给f.close()。如果您使用上下文管理器并执行以下操作:

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

它将在最后自动为您关闭文件。


0
投票

例如,您可以使用简单的密码恢复系统来扩展脚本。我认为学习可能会有用...

您可以实现一种简单的哈希系统,以避免将密码另存为纯文本。

如果要添加GUI,请考虑使用Tkinter。https://docs.python.org/3/library/tkinter.html

让我们知道。

祝您好运,并保持<3

© www.soinside.com 2019 - 2024. All rights reserved.