【发布时间】:2019-02-13 11:18:15
【问题描述】:
所以,基本上,我需要我的 python 代码从外部文件中读取每一行并删除逗号后的所有内容。然后,它应该使用新字符串检查用户输入。
就像一个验证过程,用户注册,然后代码检查用户名是否被使用
但在外部文件中,用户详细信息存储为: '用户名,密码'
这是我的代码:
LogFile = 'accounts.txt'
invalidUserName = False
Uusername = input("Please enter your desired username: ")
while len(Uusername) < 3 or len(Uusername) > 16:
Uusername = input("Username is not between the length of 3 and 16, please re-enter: ")
with open(LogFile) as f:
content = f.readlines()
contnt = [l.strip() for l in content if l.strip()]
passlist = []
userlist = []
for line in content:
try:
userlist.append(line.split(",")[0])
passlist.append(line.split(",")[1])
except IndexError:
pass
for username in userlist:
while username == Uusername:
Uusername = input(('Username is taken, please enter another one: '))
invalidUserName = True
if not invalidUserName:
password = input("Please enter your desired password: ")
while len(password) < 3 or len(password) > 16:
password = input("Password is not between the length of 3 and 16, please re-enter: ")
while password == username:
password = input("Password cannot be the same as the username, please re-enter: ")
VerPW = input("Please verify your password: ")
while VerPW != password:
VerPW = input("Passwords do not match, try again: ")
file = open ('accounts.txt','a')
file.write (username +','+password + "\n")
file.flush()
file.close
print ("Details stored successfully")
在第 5 行之后,我需要代码来检查外部文件中的用户名,逗号后没有任何内容,如果用户名已经存在,则要求用户再次输入
【问题讨论】:
-
天哪,这些密码是明文吗?
-
现在,到目前为止,您尝试了什么?给我们看一些代码。您遇到什么具体问题?见How to ask
标签: python python-3.x coding-style