DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Adil
Adil

Posted on

How can I optimize my login system code

I currently started to learn Python, and I love to practice so I can learn a bit more. I have written this login system code in Python in order to learn a bit more programming in Python. It works, (I don't know why ๐Ÿ˜‚) and I was wondering if that's how I should do it or not. And also if I can maybe optimize it.
The next step, is that I want to implement a registration system by using the hashlib module (that I just heard about it, so it can hash the passwords, and also have a json file that stores all the usernames and passwords) of course I know it's not secure nor anything but just for practicing on my computer.

THE CODE :

# Login Page
def Menu():
    choice = input("LOGIN (L) |OR| REGISTER (R)")
    if choice == "L":
        LoginPage()
    elif choice == "R":
        Register()
    else:
        return False

def Register():
    print("Currently registration are closed. Please try again later.")
failed_attempts = 3
def LoginPage():
    global failed_attempts
    global username
    username = input("Username :")
    password = input("Password :")
    if username == "admin" and password == "lolipop":
        Success()
    else:
        failed_attempts -= 1
        Failed()

def Failed():
    print("Username or password incorrect.")
    if failed_attempts >= 1:
        #LoginPage()
        print(f"Please try again \nYou have {failed_attempts} attempts remaining.")
        LoginPage()
    elif failed_attempts == 0:
        print("Please try again later.")

def Success():
    print("You are successfully logged in", username)

while Menu() == False:
    Menu()
Enter fullscreen mode Exit fullscreen mode

Thank you :)

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. ๐Ÿ›