Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python Login System Tutorial
Nixify (7)

How To Make A Login System In Python

By @Nixify

To Just Copy Paste, Go To Main.py
Or Use The Tutorial


First, Import The Modules

import os, time
from replit import db

We Import The Repl DB Because We Are To Lazy To Make One

Then Make A Function For Clearing

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

We Do This To Clear Faster, And You Might Need In What You Are Making

Next, Make A Function, Call Is LS()

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  ...

Next, Make A Variable

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

We Do This So If This System Is In A Game, Or Something Else, The Program Will Know If You Are Logged In

Asking The User

Use The Input Function To Ask

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")

We Use \n, Because It Adds A New Line, And Looks Nicer

Checking User Response

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    ...
  elif ls == "2":
    ...
  elif ls == "3":
    ...
  else:
    print("Not An Option!)

Now, Make The Sign Up Form

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
  elif ls == "2":
    ...
  elif ls == "3":
    ...
  else:
    print("Not An Option!)

Now, We Need To Use The Repl DB To Store The Data

To Do That, We Do db[key] = value

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
    db[susername] = spassword
    print("Successfully Logged In!")
    LS()
  elif ls == "2":
    ...
  elif ls == "3":
    ...
  else:
    print("Not An Option!)

Now The Data Is Stored, The User Knows He/She Is Signed Up, And It Will Go Back To The 3 Options

Now For The Login

Lets Ask Teh User For Username And Password

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
    db[susername] = spassword
    print("Successfully Logged In!")
    LS()
  elif ls == "2":
    username = input("Username: ")
    password = input("Password: ")
  elif ls == "3":
    ...
  else:
    print("Not An Option!)

Now Lets Check Their Response

First, We Need To Get All The Keys, And Check If The Inputed Username Is In The Keys

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
    db[susername] = spassword
    print("Successfully Logged In!")
    LS()
  elif ls == "2":
    username = input("Username: ")
    password = input("Password: ")
    allUsernames = db.keys()
    if username in allUsernames:
      ...
  elif ls == "3":
    ...
  else:
    print("Not An Option!)

Now, Lets Check The Password

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
    db[susername] = spassword
    print("Successfully Logged In!")
    LS()
  elif ls == "2":
    username = input("Username: ")
    password = input("Password: ")
    allUsernames = db.keys()
    if username in allUsernames:
      realpassword = db[username]
      if password == realpassword:
        print("Log In Successful!")
        time.sleep(1)
        clear()
  elif ls == "3":
    ...
  else:
    print("Not An Option!)

But What If The Username Or Password Is Wrong
Then Just Tell The User

Telling User If Password Or Username Is Wrong

Use A Print Statement, Then Clear The Screen

import os, time
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
    db[susername] = spassword
    print("Successfully Logged In!")
    LS()
  elif ls == "2":
    username = input("Username: ")
    password = input("Password: ")
    allUsernames = db.keys()
    if username in allUsernames:
      realpassword = db[username]
      if password == realpassword:
        print("Log In Successful!")
        time.sleep(1)
        clear()
      else:
        print("Password Is Wrong!")
        time.sleep(1)
        clear()
    else:
      print("Username Not Found!")
      time.sleep(1)
      clear()
  elif ls == "3":
    ...
  else:
    print("Not An Option!")

The Third Option

Since The Third Option Is For Not Logging In, Just Give A Random Name
Import The Rnadom Module

import os, time, random
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
    db[susername] = spassword
    print("Successfully Logged In!")
    LS()
  elif ls == "2":
    username = input("Username: ")
    password = input("Password: ")
    allUsernames = db.keys()
    if username in allUsernames:
      realpassword = db[username]
      if password == realpassword:
        print("Log In Successful!")
        time.sleep(1)
        clear()
      else:
        print("Password Is Wrong!")
        time.sleep(1)
        clear()
    else:
      print("Username Not Found!")
      time.sleep(1)
      clear()
  elif ls == "3":
    guestusername = "Guest" + random.randint(0, 999)
  else:
    print("Not An Option!)

Finishing Up

Change The loggedin Variable After Logging In

import os, time, random
from replit import db

def clear():
  os.system('clear')
  time.sleep(1)

def LS():
  loggedin = 0

  ls = input("Do You Want To\n[1] Sign Up\n[2] Login\n[3] Continue Without Login\n")
  if ls == "1":
    susername = input("Username: ")
    spassword = input("Password: ")
    db[susername] = spassword
    print("Successfully Logged In!")
    LS()
  elif ls == "2":
    username = input("Username: ")
    password = input("Password: ")
    allUsernames = db.keys()
    if username in allUsernames:
      realpassword = db[username]
      if password == realpassword:
        print("Log In Successful!")
        loggedin = 1
        time.sleep(1)
        clear()
      else:
        print("Password Is Wrong!")
        time.sleep(1)
        clear()
    else:
      print("Username Not Found!")
      time.sleep(1)
      clear()
  elif ls == "3":
    loggedin = 0
    guestusername = "Guest" + str(random.randint(0, 999))
    print("Your Username Is" + guestusername)
  else:
    print("Not An Option!")

Notice:

There Were Some Changes While I Was Making This. Final Version Is Under The Header Finishing Up, Or Go To main.py

And Your Done!

You Can Try How It Works By Running main.py In This Program

Good Luck Coding!

Bye 👋