1
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):"吗?

4

2 回答 2

0

试试下面的代码,

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
于 2020-01-06T08:06:03.893 回答
-1

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

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
于 2020-01-06T08:39:40.233 回答