首页 > 解决方案 > 如果帐户与 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):"吗?

标签: pythonfile-writinglogin-system

解决方案


试试下面的代码,

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

推荐阅读