如果帐户与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):"

icem_net 回答:如果帐户与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
    if any(account == line.strip() for line in acc):
        print("Logged in")
        loggedin = True
,

使用所有帐户创建一个字符串,并删除换行符,然后在合并的帐户中检查所需的帐户。

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()
    all_accounts = "".join(info).replace("\n","")  # removes newline characters
    if account in all_accounts:
      print("Logged in")
      loggedin = True
本文链接:https://www.f2er.com/2814839.html

大家都在问