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

你可以做很多改进。

首先,将功能拆分为更小的功能。在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")记住以后一定要打电话给{}。

如果您使用上下文管理器并按以下方式执行:

^{pr2}$

最后它会自动关闭文件。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值