How do you check passwords in database?

Question:
How do i save passwords in database and use an if to login?
Here’s my current code:

def newgame():
  clear()
  pprint(Fore.ORCHID+"Type in your username.")
  username = input(Fore.ORCHID+"")
  clear()
  pprint(Fore.ORCHID+"Type in your name.")
  name = input(Fore.ORCHID+"")
  clear()
  pprint(Fore.ORCHID+"Type in your password.(You must remember it later on)")
  password = input(Fore.ORCHID+"")
  clear()
  pprint("Creating a new XXMadGames account...")
  db[{username}] = {username}, {name}, {password}
  time.sleep(1)
  pprint("Account created.")
  exit()
def loadgame2():
  if loadusername in db[{loadusername}]:
    pprint("Type in your password to continue.")
    loadpassword = input(Fore.ORCHID+"")
    db.check[{loadusername}] [{password}]
    if loadpassword == db:
      pprint("Loading...")
      exit()
def loadgame():
  clear()
  pprint(Fore.ORCHID+"Type in your username.")
  loadusername = input(Fore.ORCHID+"")
  if loadusername in db:
    clear()
    loadgame2()
  else:
    pprint("No such account.")
    time.sleep(1)
    loadgame()

Hey, are you using ReplDB here? If so, you can do you can manipulate the database as you would a Python dictionary. Here’s an example:

from replit import db
from getpass import getpass

def sign_up():
    username = input('What is your username? ')
    password = getpass('What is your password? ')
    db[username] = password

def login():
    username = input('What is your username? ')
    password = getpass('What is your password? ')
    correct_pass = db.get(username, None)
    if password == correct_pass:
        print('Correct password')
    else:
        print('Incorrect password')

sign_up()
login()

Hope that helps!

1 Like