I am trying to ask a user for a username and password and check a file with user name and passwords to see if it exists if it does it just says welcome if it doesnt tells the user he put in the wrong info. The file is just a simple text file with format "username,password" as shown below
SSD,adgEgd
aegag,asdhasdh
here is my code
class Account():
def __init__(self, name, password):
self.name = name
self.password = password
username_input = input("Enter the name: ")
userpassword_input = input("Enter password: ")
file = open('account information.txt', 'r')
data = []
for lines in file:
temp = lines.split(',')
data.append(Account(temp[0], temp[1]))
file.close()
isExsits = ' '
for d in data:
if username_input == d.name and userpassword_input == d.password:
isExsits = 'huzzah'
print(isExsits)
It identifies the username but not the password
CodePudding user response:
There is a newline character in the password and it looks like adgEgd\n after reading from file.
You can get rid from it by using rstrip
data.append(Account(temp[0], temp[1].rstrip()))
CodePudding user response:
This works as you intended, it iterates over all accounts in the account_information.txt
file and creates an Account
object for each of them which is added to the data
list.
Afterwards, we iterate through every account and check to see if the credentials provided by the user match any within the list.
class Account():
def __init__(self, name, password):
self.name = name
self.password = password
username_input = input("Enter the name: ")
userpassword_input = input("Enter password: ")
data = []
with open("account_information.txt") as file:
for line in file.readlines():
credentials = line.rstrip().split(",")
data.append(Account(credentials[0], credentials[1]))
accountExists = False
for account in data:
if (account.name == username_input) and (account.password == userpassword_input):
accountExists = True
print(accountExists)
CodePudding user response:
It seems to working fine like this, so I would likewise say it's probably the newlines or other extraneous characters at the end of the lines that are throwing off the calculation.
user_pass_list = [
('SSD', 'adgEgd'),
('aegag', 'asdhasdh')
]
username = 'SSD'
password = 'adgEgd'
exists = False
for d in user_pass_list:
if username == d[0] and password == d[1]:
exists = True
print(exists) # True
Another approach can be to just do an in
check, so that we don't need to iterate over user_pass_list
for example:
user_pass_list = [
('SSD', 'adgEgd'),
('aegag', 'asdhasdh')
]
username = 'SSD'
password = 'adgegd' # casing is different
exists = (username, password) in user_pass_list
assert exists is False # True
With the Account
class example from the original question, reformatted as a dataclass for slightly cleaner code:
from dataclasses import dataclass
# same as `@dataclass(eq=True)`, so an equals (__eq__) method
# is automatically generated for the class.
@dataclass
class Account:
name: str
password: str
user_pass_list = [
Account('SSD', 'adgEgd'),
Account('aegag', 'asdhasdh')
]
username = 'SSD'
password = 'adgEgd'
exists = Account(username, password) in user_pass_list
assert exists is True # True