1
\$\begingroup\$

This is my first program that I wrote on my own. Is there something I can improve upon?

There are a few things that still need to be added, such as:

  • Confirm password while registering
  • Adding a way to get the forgotten password using the email ID

But its functionality is minimally complete. Are there any things which professionals see that I can improve or change in this program?

Main File

import Functions

start = ""

while start == "" :
    start = input("Type S to Create a Account and Type L to login into your Account:  ")
    Username_List = Functions.username_list()
    if start == "S" or start == "s":
        username = input("Enter a Username: ")
        for user in Username_List:
            while user == username:
                username = input("Username Exists, Try Again: ")
        password = input("Enter a Password: ")
        email_adress = input("Enter Email ID: ")
        Functions.sign_up(username,password,email_adress)
        start = ""

    elif start == "L" or start == "l":
        login_username = input("Enter Your Username: ")
        while login_username not in Username_List:
            print('Username Does not Exist.')
            user_input = input("Type R to retry and X to exit: ")
            if user_input == "r" or user_input == "R":
                login_username = input("Enter Your Username: ")
            if user_input == "x" or user_input == "X":
                break
        if login_username not in Username_List:
            print("Invalid Session \n\n")
            start = ""
        else:
            no = Username_List.index(login_username)
            password = input("Enter a Password: ")
            Functions.extract_matrix(login_username,password,no)
            start = ""

    else:
        print("Invalid Session \n\n")
        start = ""

Functions.py

import csv
def sign_up(username,password,email):

    filename = "signup_info.csv"
    data = [[username,password,email]]
    # writing to csv file
    with open(filename, 'a', newline='') as csvfile:
        # creating a csv dict writer object
        writer = csv.writer(csvfile)

        # writing data rows
        writer.writerows(data)

def username_list():
    username_list=[]
    with open('signup_info.csv','r') as f:
        for line in f :
            username_list.append(line.split(',')[0])
        return username_list

def extract_matrix(username,password,no):
    password_list = []
    email_list = []
    username_list = []
    with open('signup_info.csv','r') as f:
            for line in f:
                password_list.append(line.split(',')[1])
            for line in f:
                email_list.append(line.split(',')[2])
            for line in f:
                username_list.append(line.split(',')[0])

    data_password = password_list[no]
    while password != data_password:
        print('Incorrect Password')
        user_input = input ("Type R to retry and X to exit: ")
        if user_input == "r" or user_input == "R":
            password = input("Enter Password: ")
        if user_input == "x" or user_input == "X":
            break
    if password == data_password:
       print("Login Successful")
    else:
        print("Invalid Session \n\n")
\$\endgroup\$
0

2 Answers 2

0
\$\begingroup\$

For your very first program, this is very impressive!

Your main file should move its code into one or more functions, and call the top-level function via

if __name__ == '__main__':
    main()

because - if another programmer wants to call into your code but execute parts of it selectively - this will let them.

Your

if start == "S" or start == "s":

pattern is more easily expressed as

if start.lower() == 's':

Does your main loop have an exit feature? Currently it seems not, so you should add one.

Don't use input to capture a password; use secrets instead.

You use the csv module to write your data, but

  • your file is missing a header, which you should add; and
  • you do not properly use the same csv module when reading. You should call into that instead of splitting. Currently, what happens if you enter a username with a comma in it? I suspect that will break your existing parsing but will be fine when using the csv module.

You need to hash and salt your passwords. This is crucial and cannot be compromised on, no matter the production status or experience level. If you don't know what these mean, it's time for some internet research. If you're not comfortable applying a little crypto, managing your own password storage is not a good choice and you should let a stock secure wallet or credential store library do this for you.

\$\endgroup\$
-3
\$\begingroup\$

User Interface

If you really want this to look professional, you can try using tkinter to make it a GUI. Typing "S" and "L" is unprofessional if you are going to be using this in a production environment. If it's meant to be a simple CLI by all means this is good. Maybe you can integrate this with Telegram API or Discord API

Security

You can also improve it by using a cryptography module to encrypt passwords before storage. Not neccessary at all if not for use in the real world.

\$\endgroup\$
1
  • \$\begingroup\$ Passwords should be stored salted and hashed, never encrypted. \$\endgroup\$
    – TomG
    Jul 4, 2021 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.