Is there some kind of code that allows me to print ("Logged in") if the account is the exact same as a line in the txt document?

61 views Asked by At
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

is there any possible way for me to make the "if(account in line):" say, "if(account is exactly the same as in any of the lines here):"?

2

There are 2 answers

1
Shibiraj On

Try this code below,

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
0
Alex Kiura On

Create a string with all of the accounts and remove the newline characters then check for your desired account in the combined accounts.

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