我是新手,目前我正在尝试为我的作业创建一个注册和登录系统,我这样做了。
def signUp(): #USER SIGN UP
name=(str(input("Name: ")))
intake=(str(input("Intake: ")))
hp=(str(input("Phone Number: ")))
email=(str(input("E-mail: ")))
tp=(str(input("Student ID: ")))
pw=(str(input("Password: ")))
OccupantsData=[name,intake,hp,email,tp,pw]
file=open("Database.txt","a")
file.write(str(OccupantsData)+"\n")
file.close()
当我运行此代码时,它会将所有输入保存到' Database.txt'像这样
['James', 'Jul17', '1234', 'etc@etc.com', 'TP1234', 'password']
['Jon', 'Sep17', '5567', 'etc1@etc.com', 'TP2345', 'passwords']
['Han', 'Oct17', '7554', 'etc2@etc.com', 'TP5546', 'passwords1']
['Klye', 'Oct17', '2234', 'etc3@etc.com', 'TP0094', 'passwords2']
现在,我不确定登录的代码是什么...它应该接受TPnumber并确保它与行上的密码匹配...当我以这种方式编码时,它只适用于第一行的TPnumber和密码,对其他人不起作用......
def logIn(): #USER SIGN IN
TP=str(input("Please input TP Number:"))
password=input("Please input password:")
f=open("Database.txt","r")
user=f.read()
if (TP) in user:
if password in user:
print("Welcome")
else:
print ("Username or Password doesn't match")
logIn()
else:
print("Error")
logIn()
我该怎么做才能让它读取输入的用户名和密码,而不仅仅是第一行?
答案 0 :(得分:1)
我建议输出json文件而不是文本文件,并将数据作为字典查找。但是,由于您编写的行看起来像列表,因此您可以将字符串评估为具有ast.literal_eval()
的实际列表。
<强>鉴于强>
文件Database.txt
['James', 'Jul17', '1234', 'etc@etc.com', 'TP1234', 'password']
['Jon', 'Sep17', '5567', 'etc1@etc.com', 'TP2345', 'passwords']
['Han', 'Oct17', '7554', 'etc2@etc.com', 'TP5546', 'passwords1']
['Klye', 'Oct17', '2234', 'etc3@etc.com', 'TP0094', 'passwords2']
从这个重构函数创建:
def signup():
"""User sign up."""
name = str(input("Name: "))
intake = (str(input("Intake: ")))
hp = str(input("Phone Number: "))
email = str(input("E-mail: "))
tp = str(input("Student ID: "))
pw = str(input("Password: "))
occupants_data = [name, intake, hp, email, tp, pw]
# Safely open/close files
with open("Database.txt", "a") as f:
f.write(str(occupants_data) + "\n")
<强>代码强>
from ast import literal_eval
def login():
"""User sign in."""
tp = str(input("Please input TP Number: "))
password = str(input("Please input password: "))
with open("Database.txt", "r") as f:
for line in f.readlines():
if not line or line =="\n":
continue
user_data = literal_eval(line) # convert string-list to a list
if tp == user_data[-2]:
if password == user_data[-1]:
print("Welcome")
return
else:
print ("Username or Password doesn't match")
return
print("Error")
<强>演示强>
<强>详情
signup()
函数由以下内容重构:
with
语句用于安全地打开和关闭文件这可用于生成Database.txt
文件。
login()
函数由以下内容重构:
with
语句来处理文件您可能会考虑的下一个概念是exception handling引发错误而不是打印错误并处理用户KeyboardInterupt
以退出提示。
答案 1 :(得分:0)
为了使输入与一个或多个 txt 文件内容相匹配,您需要循环它。此外,要检查密码是否在列表中,您必须将其转换为 string 。所以你的登录代码将成为:
(defun deep-reverse (tree)
(cond ((zerop (length tree)) nil)
((and (= 1 (length tree)) (atom (car tree))) tree)
((consp (car tree)) (append (deep-reverse (cdr tree))
(list (deep-reverse (car tree)))))
(t (append (deep-reverse (cdr tree)) (list (car tree))))))
祝你好运!
答案 2 :(得分:0)
input()
返回一个字符串,因此无需使用str()
进行显式转换。在处理文件对象时更好地使用with statement
,因为它会自动关闭它们。
定义signUp()
:
def signUp(): #USER SIGN UP
name = input("Name: ")
intake = input("Intake: ")
hp = input("Phone Number: ")
email = input("E-mail: ")
tp = input("Student ID: ")
pw = input("Password: ")
OccupantsData = [name, intake, hp, email, tp, pw]
with open("Database.txt", "a") as db:
db.write(' '.join(OccupantsData)) # Writing it as a string
致电signUp()
:
signUp()
Name: x
Intake: something
Phone Number: 123-456-7890
E-mail: x@email.com
Student ID: x123
Password: password
>>>
定义logIn()
:
def logIn(): #USER SIGN IN
TP = input("Please input TP Number: ")
password = input("Please input password: ")
with open("Database.txt") as db:
for line in db:
line = line.split(' ') # Making it a list
if TP in line:
if password in line:
print("Welcome")
else:
print ("Username or Password doesn't match")
logIn()
else:
print("Error")
logIn()
致电logIn()
:
logIn()
Please input TP Number: x123
Please input password: pass
Username or Password doesn't match
Please input TP Number: x123
Please input password: password
Welcome
>>>
在处理文件对象时,最好使用with关键字。优点是文件在套件完成后正确关闭,即使在某个时刻引发了异常。