使用文本文件创建 Python 登录系统
Creating a Python Log-In system using text files
我正在尝试使用文本文件在 python 中创建一个基本的注册和登录系统。我还希望程序一次性 运行,这意味着我希望用户能够注册,然后立即继续登录,而不是让他们返回并再次 运行 代码.但是,我编写的代码由于某种原因无法做到这一点。我在注册函数的末尾添加了“signin()”,以允许用户在成功注册后继续登录,但这样做会引发错误。相反,我必须再次 运行 代码才能识别新注册。
代码如下:
def signin():
q = 0
user = input('Username: ')
password = input('Password: ')
with open('username.txt', mode='r')as names:
for i in names:
foundname = names.readlines()
with open('password.txt', mode='r')as passwords:
for j in passwords:
foundpass = passwords.readlines()
if user + "\n" in foundname: # ReadLines() adds newlines
user_num = foundname.index(user + "\n")
for i in range (0,user_num):
if password + "\n" == foundpass[i]:
print("Login Successful.")
print("Incorrect Password. Try Again.")
signin()
else:
print("Username not found. Try again")
signin()
def signup():
with open('username.txt', mode='a')as names:
print("Please provide the following details to sign up")
name = str(input("Username: "))
names.write(f"{name}\n")
with open('password.txt', mode='a')as passwords:
password = (input("Password: "))
passwords.write(f'{password}\n')
print("signup successful. You can sign-In Now.")
signin()
signup()
这是我不断收到的输出和错误:
Please provide the following details to sign up
Username: abcdef
Password: password1
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Traceback (most recent call last):
File "D:/student.py", line 34, in <module>
signup()
File "D:/student.py", line 33, in signup
signin()
File "D:student.py", line 14, in signin
if password + "\n" == foundpass[i]:
IndexError: list index out of range
Process finished with exit code 1
但是当我再次 运行 代码时,我上次注册时使用的凭据似乎有效。
Please provide the following details to sign up
Username: jkl
Password: mno
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Login Successful.
非常感谢!
with open('password.txt', mode='a')as passwords:
...
signin()
文件没有关闭,您尝试登录,因此文件指向最后一个索引,您无法读取任何数据。将其更改为
with open('password.txt', mode='a')as passwords:
password = (input("Password: "))
passwords.write(f'{password}\n')
print("signup successful. You can sign-In Now.")
signin()
应该可以解决这个问题。
我正在尝试使用文本文件在 python 中创建一个基本的注册和登录系统。我还希望程序一次性 运行,这意味着我希望用户能够注册,然后立即继续登录,而不是让他们返回并再次 运行 代码.但是,我编写的代码由于某种原因无法做到这一点。我在注册函数的末尾添加了“signin()”,以允许用户在成功注册后继续登录,但这样做会引发错误。相反,我必须再次 运行 代码才能识别新注册。
代码如下:
def signin():
q = 0
user = input('Username: ')
password = input('Password: ')
with open('username.txt', mode='r')as names:
for i in names:
foundname = names.readlines()
with open('password.txt', mode='r')as passwords:
for j in passwords:
foundpass = passwords.readlines()
if user + "\n" in foundname: # ReadLines() adds newlines
user_num = foundname.index(user + "\n")
for i in range (0,user_num):
if password + "\n" == foundpass[i]:
print("Login Successful.")
print("Incorrect Password. Try Again.")
signin()
else:
print("Username not found. Try again")
signin()
def signup():
with open('username.txt', mode='a')as names:
print("Please provide the following details to sign up")
name = str(input("Username: "))
names.write(f"{name}\n")
with open('password.txt', mode='a')as passwords:
password = (input("Password: "))
passwords.write(f'{password}\n')
print("signup successful. You can sign-In Now.")
signin()
signup()
这是我不断收到的输出和错误:
Please provide the following details to sign up
Username: abcdef
Password: password1
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Traceback (most recent call last):
File "D:/student.py", line 34, in <module>
signup()
File "D:/student.py", line 33, in signup
signin()
File "D:student.py", line 14, in signin
if password + "\n" == foundpass[i]:
IndexError: list index out of range
Process finished with exit code 1
但是当我再次 运行 代码时,我上次注册时使用的凭据似乎有效。
Please provide the following details to sign up
Username: jkl
Password: mno
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Login Successful.
非常感谢!
with open('password.txt', mode='a')as passwords:
...
signin()
文件没有关闭,您尝试登录,因此文件指向最后一个索引,您无法读取任何数据。将其更改为
with open('password.txt', mode='a')as passwords:
password = (input("Password: "))
passwords.write(f'{password}\n')
print("signup successful. You can sign-In Now.")
signin()
应该可以解决这个问题。