python - 如果帐户与 txt 文档中的一行完全相同,是否有某种代码允许我打印(“登录”)?
问题描述
if(choice1 == "/login"):
uname = input("Username: ")
pword = input("Password: ")
account = str(uname) + str(pword)
with open("accounts.txt") as acc: #CHECKS IF ACCOUNT IS IN DIRECTORY
info = acc.readlines()
for line in info:
if(account in line):
print("Logged in")
loggedin = True
break
有什么方法可以让我"if(account in line):"
说出来"if(account is exactly the same as in any of the lines here):"
吗?
解决方案
试试下面的代码,
if(choice1 == "/login"):
uname = input("Username: ")
pword = input("Password: ")
account = str(uname) + str(pword)
with open("accounts.txt") as acc: #CHECKS IF ACCOUNT IS IN DIRECTORY
if any(account == line.strip() for line in acc):
print("Logged in")
loggedin = True
推荐阅读
- java - 如何以编程方式移动图像android
- java - 为什么Android Studio在选择图像后改变图像旋转
- pharo - Seaside 中嵌套的#call: 的数量是否有限制?
- node.js - 当字符串超过模型中定义的大小时,如何使 sequelize.js 截断字符串?
- php - 将图像复制到存储文件夹
- .net - Azure 逻辑应用程序 502 -“错误网关”错误
- ios - UIModalTransitionStyle 水平翻转问题
- python - 根据与指定值的接近程度对 Pandas DataFrame 进行排名
- c++ - 使用winapi模拟鼠标点击并在窗口中滚动(android模拟器)
- html - 我在让前 50% 的人在 CSS 中工作时遇到问题