Python密码拒绝验证
我的代码拒绝验证输入的用户名和密码,即使输入正确 代码包括在下面;我哪里做错了?如果是的话,你能告诉我在哪里吗 干杯Python密码拒绝验证,python,validation,passwords,Python,Validation,Passwords,我的代码拒绝验证输入的用户名和密码,即使输入正确 代码包括在下面;我哪里做错了?如果是的话,你能告诉我在哪里吗 干杯 import time print ("Before playing, you must register.") time.sleep(1) username = open("username.txt","w+") password = open("password.txt","w+") print ("Please enter your desired username."
import time
print ("Before playing, you must register.")
time.sleep(1)
username = open("username.txt","w+")
password = open("password.txt","w+")
print ("Please enter your desired username.")
username_input = input("> ")
print ("Please enter your desired password.")
password_input = input("> ")
username.write(username_input)
password.write(password_input)
username.close()
password.close()
time.sleep(1)
print ("Now, we must authenticate your username and password.")
time.sleep(0.5)
print ("Please input your username.")
u_input = input ('> ')
print ("Please input your password.")
p_input = input ('> ')
username.open("username.txt","r")
password.open("password.txt","r")
u_contents = username.read()
p_contents = password.read()
if u_input == u_contents:
print ("Username authenticated.")
if p_input == p_contents:
print ("Password authenticated.")
else:
print ("Incorrect username or password.")
username.close()
password.close()
即使你打电话给write,内容实际上还没有写完。在关闭或刷新文件或程序退出之前,文件内容不会写入磁盘
写入文件后关闭它们。由于某些原因,w+不工作。我张贴的完全工作的代码,我已经改变了你的。与/一起使用openimport time总是很有用的
import time
print ("Before playing, you must register.")
time.sleep(1)
print ("Please enter your desired username.")
username_input = input("> ")
print ("Please enter your desired password.")
password_input = input("> ")
with open('username.txt', 'w') as u:
u.write(username_input)
u.flush()
u.close()
with open('password.txt', 'w') as p:
p.write(password_input)
p.flush()
p.close()
time.sleep(1)
print ("Now, we must authenticate your username and password.")
time.sleep(0.5)
print ("Please input your username.")
u_input = input ('> ')
print ("Please input your password.")
p_input = input ('> ')
username=''
password=''
with open('xx.txt', 'r') as u:
username = u.read()
u.close()
with open('xxx.txt', 'r') as p:
password = p.read()
p.close()
if u_input == username:
print ("Username authenticated.")
if p_input == password:
print ("Password authenticated.")
else:
print ("Incorrect username or password.")
输出:
.read读取整个文件,包括换行符。请注意:不要像您现在这样将用户凭据存储在纯文本文件中。您应该关闭文件,然后重新读取以访问其内容。@TheIncorrigible1没有向该文件写入换行符。问题是输出还没有写入文件。@JohnGordon没有写入。好电话。我建议OP和你一起学习keyword@TheIncorrigible1这只是为了学校,不是真的:我现在得到了这个错误:回溯最近的调用last:File main.py,第26行,在username.openusername.txt,r AttributeError:“_io.TextIOWrapper”对象没有属性“open”username已经是一个打开的文件;你应该调用username.close来关闭文件。我已经关闭了它,我在写入后立即插入了close;当我试图再次打开它时,编辑您的问题以显示更新的代码。很难从简短的评论中分辨出你到底改变了什么;现在应该在上面了
C:\Users\Documents>py test.py
Before playing, you must register.
Please enter your desired username.
> mike
Please enter your desired password.
> mike123
Now, we must authenticate your username and password.
Please input your username.
> mike
Please input your password.
> mike123
Username authenticated.
Password authenticated.