python - 我的登录系统将无法工作 - 无论输入任何用户密码
问题描述
我的注册和登录身份验证系统无法正常工作。我的注册问题已解决,但我的登录有问题。代码要么让我通过并访问帐户,要么不会,这取决于代码。但无论何时我尝试修复它,输出都是两个选项之一。总是,不管我输入什么密码。
用户名和密码存储在一个 txt 文件中,如下所示:
John Appleseed:hisSuperSecretPassword
JohnDoe:1234
登录代码:
found = False
username = input("Enter your username:\n")
file = open("account.txt", "r+")
for line in file:
if line.split(':')[0] == username:
found = True
if found == True:
password = input("Enter your password:\n")
for counter, line in enumerate(file):
if line.strip() == username + ":" + password:
print("You have signed in.")
else:
print("Password incorrect. Program closing.")
sys.exit()
else:
print("Username not valid.")
sys.exit()
任何人都可以帮忙吗?运行 Python 3.9.2。
解决方案
这是我为工作而调整的东西....
import sys
found = False
username = input("Enter your username:\n")
file = open("account.txt", "r+")
for line in file:
if line.split(':')[0] == username:
account_details = line.split(':')
found = True
if found == True:
password = input("Enter your password:\n")
if account_details[1].strip() == password:
print("You have signed in.")
else:
print("Password incorrect. Program closing.")
sys.exit()
else:
print("Username not valid.")
sys.exit()
推荐阅读
- mysql - 在 windows 命令中将 mysql 表导入和导出到 xml 文件?
- java - Java 8 中的比较器接口如何成为@FunctionalInterface
- php - 包含 PHP 文件时遇到问题
- bash - 无法在 bash 脚本中循环运行“gradle”相关命令
- react-native - 如何通过 react-native 中的 Youtube API 检索 Youtube 频道视频列表
- java - 如果请求参数超过 15,我应该使用 POST 或 GET 什么?
- javascript - 如何存储自定义 css 。laravel 中的 js 文件并在视图中调用它
- php - 上传后如何从文件名中删除特殊字符
- android - 如何从通知或应用程序图标点击 android 知道应用程序打开?
- mysql - 无法连接到在不同机器上的容器上运行的远程 mySQL 数据库