i have made simple python login nothing impressive but i have the feeling that i could have done it differently.
I saw some people saying that i should use text files but i tried and i didn't quite work out with iteration .
How would you approach it using a text file ?
print('WELCOME USER , Please Enter Your Login Information')
class Login:
def __init__(self):
self.data = {}
self.username = input('Username : ')
self.password = input('Password : ')
def login_check(self):
key1, value1 = self.username , self.password
if key1 in self.data and value1 == self.data[key1]:
print(f'\nWelcome Back {self.username}')
else:
print("\nWrong Username Or Password")
ask = input("\nAre You A New User? Y/N : ")
if ask == "y":
self.new_user()
if ask == 'n':
check_username = input("\nUsername : ")
check_password = input("Password : ")
key, value = check_username , check_password
if key in self.data and value == self.data[key]:
print(f"\nWELCOME {check_username}!!")
def new_user(self):
new_username = input('\nPlease Enter A New Username : ')
new_password = input('Please Enter A New Password : ')
self.data[new_username] = new_password
check_username = input("\nUsername : ")
check_password = input("Password : ")
key , value = check_username , check_password
if key in self.data and value == self.data[key] :
print(f"\nWELCOME {check_username}!!")
else:
self.login_check()
main = Login()
main.login_check()
-
\$\begingroup\$ What is the program supposed to do? Where will the user names and passwords be stored? Even if this script exists purely for educational purposes, at least declare some kind of a basic plan (it's ok to adjust the plan as you learn more): for example, "Passwords will be in a text file, a JSON file, a database, whatever. Script will support 3 actions: add user, delete user, or login". Some clarity about the goal will help you and any people willing to review your code. \$\endgroup\$FMc– FMc2020年08月30日 01:19:50 +00:00Commented Aug 30, 2020 at 1:19
-
\$\begingroup\$ @FMc , this isn't the full project . This is actually just a test one that is why it has a few holes like that it doesn't save permanently . But that is why i am asking how you would approach this with a text file. Sorry for the misunderstanding. \$\endgroup\$bliboy– bliboy2020年08月30日 07:05:47 +00:00Commented Aug 30, 2020 at 7:05
1 Answer 1
First of all I recommend you to separate your class to two classes. Each class must have only one responsibility. Now your class make two different things - store and validate passwords, and working with user input/output.
Let's try to fix it and separate your code to two classes:
class LoginStore:
def __init__(self):
self.data = {}
def add_user(self, login, password):
if login in self.data:
raise AssertionError('User already exists')
self.data[login] = password
def check_user(self, login, password):
if not login in self.data:
return False
if self.data[login] != password:
return False
return True
class LoginManager:
def __init__(self):
self.store = LoginStore()
def _ask_input_and_password(self):
username = input("username: ")
password = input("password: ")
return username, password
def login_check(self):
username, password = self._ask_input_and_password()
while not self.store.check_user(username, password):
print("Wrong username or password")
if input("Are you a new user?") == "y":
print("Starting registration process")
username, password = self._ask_input_and_password()
self.store.add_user(username, password)
print("Done. Try to login.")
username, password = self._ask_input_and_password()
manager = LoginManager()
manager.login_check()
-
\$\begingroup\$ is that a prefered methode or is that a personal chose. I previously made a guessing game and i heard none say anything. But if it is i will gladly implement it into to my other projects. THANKS FOR THE ADVIES. \$\endgroup\$bliboy– bliboy2020年08月29日 20:39:08 +00:00Commented Aug 29, 2020 at 20:39
-
1\$\begingroup\$ @bliboy, it is one of five principles intended to make a code more understandable en.wikipedia.org/wiki/Single-responsibility_principle \$\endgroup\$ceth– ceth2020年08月29日 20:56:19 +00:00Commented Aug 29, 2020 at 20:56
-
\$\begingroup\$ i get it now. It would be a bad design to couple two things that change for different reasons at different times and should, therefore, be in separate classes or modules. THANKS FOR THAT , YOU ARE A BIGGG HELP!! \$\endgroup\$bliboy– bliboy2020年08月29日 22:23:01 +00:00Commented Aug 29, 2020 at 22:23