python - 登录程序不起作用,无论输入什么
问题描述
登录程序,使用名为“UserAccounts.txt”的文件
def login():
print("logging in, ok")
counter = 0
while counter <= 5:
Username = input("Username:")
Password = input("Password:")
users = open("UserAccounts.txt","r")
entry = False
########### v checks file v #############
for record in users:
if record == Username:
Pass = record
if Pass == Password:
print("logged in")
entry = True
if entry == False:
print("Incorret, try again")
counter = counter + 1
print("LOCKED: Tried over 5 times")
无论输入如何,代码始终输出:
logging in, ok
Username:El
Password:Password
Incorret, try again
Username:
你知道为什么吗?(当用户和密码在文件中时)
感谢您的帮助,祝您有美好的一天!(可能很简单的问题,但大脑不工作)
解决方案
逐行阅读时,行是用逗号分隔的,所以在声明为Username
or之前需要用逗号分隔Pass
。
def login():
print("logging in, ok")
counter = 0
while counter <= 5:
Username = input("Username:")
Password = input("Password:")
users = open("UserAccounts.txt","r")
entry = False
########### v checks file v #############
for record in users:
if record.split(',')[0] == Username:
Pass = record.split(',')[1] #assuming 34 is pwd. [2] if 'Secure' is the pwd
if Pass == Password:
print("logged in")
entry = True #maybe replace this line with "return" to stop while loop
if entry == False:
print("Incorret, try again")
counter = counter + 1
推荐阅读
- .net - 使用 Microsoft Graph API .Net Sdk 时如何获取 DeleteUser 的状态
- javascript - 反应表排序不适用于计算列
- jboss - KIE Drool Workbench:由于“禁止”屏幕,无法进入工作台 UI
- autohotkey - ahk 运行 pid 与 Window Spy 中显示的不同
- python - TypeError: VideoCapture() 没有参数
- javascript - 如何在暂停一次但启用控制后仍然播放视频,addeventlistener 根本不起作用?
- html - 如何将“提交”按钮移动到 HTML 页面的右侧?
- c++ - C++ 检查空文件中的 Eof
- flutter - 将 StatefulWidget 作为参数传递并访问其构造函数
- python - 如何在 python 中添加占位符以在 mysql 数据库中创建表名?