Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 Login Program Using Dictionaries

I am trying to create a login program for a project and I can't seem to figure out two things. The first is how to have the login info stored in the dictionary after i quit the program and the second is to figure out how to quit the program correctly. Thanks! Here is the source code.

#Dictionary
users = {}
#Used to quit/create/login users
status = ""
#Start Menu:Ask if user exists or create new user
def Display_Menu():
    status =input("Are you a registered user? (Yes/No)? Press q to quit: ")
    if status == "Yes":
        Old_User()
    elif status == "No":
        New_User()
#Creates New User
def New_User():
    Create_Login =input("Create login name: ")
    if Create_Login in users:
        print ("Login name already exist!")
    else:
        Create_Password =input("Create password: ")
        users[Create_Login] = Create_Password
        print("New User created!")     
#Login if old user
def Old_User():
    login =input("Enter login name: ")
    Password =input("Enter password: ")

    if login in users and users[login] == Password:
        print("Login successful!")
    else:
        print("User doesn't exist or wrong password!")
#Quits Program
if status == "q":
    exit()
while status !="q":
    Display_Menu()
like image 418
user3006947 Avatar asked Apr 15 '26 19:04

user3006947


1 Answers

Well, python program won't store the data in the memory, so I would say, you should create a login_data.txt.

The code could look something like this

import json # may not be required as per storing method

#Dictionary
with open("login_data.txt", "r") as login_file:
    try:
        users = json.load(login_file)
    except:
        users = {}
#Used to quit/create/login users
status = ""
#Start Menu:Ask if user exists or create new user
def Display_Menu():
    status =input("Are you a registered user? (Yes/No)? Press q to quit: ")
    if status == "Yes":
        Old_User()
    elif status == "No":
        New_User()
#Creates New User
def New_User():
    Create_Login =input("Create login name: ")
    if Create_Login in users:
        print ("Login name already exist!")
    else:
        Create_Password =input("Create password: ")
        users[Create_Login] = Create_Password
        print("New User created!")     
#Login if old user
def Old_User():
    login =input("Enter login name: ")
    Password =input("Enter password: ")

    if login in users and users[login] == Password:
        print("Login successful!")
    else:
        print("User doesn't exist or wrong password!")
#Quits Program (modified)
while True:
    if status in {"q", "Q"}:
        # store the data before quitting
        with open("login_data.txt", "w") as login_file:
            json.dump(users, login_file)
        break
    else:
        Display_Menu()

As for the quitting is concerned, the python program automatically quits after executing all the code. So, after the break statement is encountered, the program falls out of the while loop & since there is no more code after that, the program automatically exits.

like image 108
shad0w_wa1k3r Avatar answered Apr 18 '26 14:04

shad0w_wa1k3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!