3
\$\begingroup\$

I'm new to programming. I've started learning Python and wrote this following code. I'd appreciate if someone reviews it and gives me feedbacks to improve the code or suggests something to learn better.

import os
import hashlib


def startup():
    file_name = 'Admin_data.txt'
    file_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name)
    with open(file_path, 'w+') as w_admin:
        print('Please select an admin name\n')
        x = input()
        print('Please select a password\n')
        y = input()
        if len(x) >= 6 and len(y) >= 8:
            raw_data = x + ':' + y
            encoded_data = raw_data.encode('utf-8')
            hashed_data = hashlib.sha224(encoded_data).hexdigest()
            #w_admin.write(x + ':' + y)
            w_admin.write(hashed_data)
            w_admin.close()
        else:
            print('Wrong combinations\nAdmin name must be 6 characters long and Password must be 8 characters long')
            startup()
     with open(file_path, 'a+') as w_read:
         w_read.read()
         w_read.close()
         print('Admin name and password have been created')
         home()


def home():
    print('Welcome to advance loginSys\nPlease select either of the following')
    print('Press "A" to Login\nPress "B" to create a new user')
    x = input().lower()
    if x == 'a':
        login()
    elif x == 'b':
        create_new_user()
    else:
        print('Invalid options selected\nTry again...')
        home()


def create_new_user():
    file_name = 'user_data.db'
    file_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name)
    with open(file_path, 'a') as w_user:
        print('Please select a username\n')
        x = input()
        print('Please select a password\n')
        y = input()
        if len(x) >= 6 and len(y) >= 8:
            raw_data = x + ':' + y
            encoded_data = raw_data.encode('utf-8')
            hashed_data = hashlib.sha224(encoded_data).hexdigest()
            w_user.write(hashed_data + '\n')
            #w_user.write(x + ':' + y + '\n')
            print('Username and password have been created successfully')
            w_user.close()
            login()
        else:
            print('Wrong combinations\nAdmin name must be 6 characters long and Password must be 8 characters long')
            create_new_user()


def login():
    print('Please identify yourself\nPress "A" if you are an admin\nPress "B" if you are a standard user')
    x = input().lower()
    if x == 'a':
        admin_login()
    elif x == 'b':
        user_login()
    else:
        print('Invalid option selected\nPlease press "A" or "B"')
        login()


def user_login():
    print('Please enter your standard username')
    x = input()
    print('Please enter your password')
    y = input()
    file_name = 'user_data.db'
    file_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name)
    with open(file_path, 'r+') as f:
        correct_user_data = f.read()
        raw_input = x + ':' + y
        encoded_input = raw_input.encode('utf-8')
        hasshed_input = hashlib.sha224(encoded_input).hexdigest()
        if hasshed_input in correct_user_data:
            print('Login successful')
            print('Welcome ' + x)
            f.close()
        else:
            print('Login failed\nEntered username and/or password were wrong\nTry again')
    user_login()


def admin_login():
    file_name = 'Admin_data.txt'
    file_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name)
    print('Please enter your admin name')
    x = input()
    print('Please enter your password')
    y = input()
    with open(file_path, 'r+') as f:
        correct_admin_data = f.read()
        raw_input = x + ':' + y
        encoded_input = raw_input.encode('utf-8')
        hasshed_input = hashlib.sha224(encoded_input).hexdigest()
        if hasshed_input == correct_admin_data:
            print('Login successful')
            print('Welcome ' + x)
            f.close()
        else:
            print('Login failed\nEntered username and/or password were wrong\nTry again')
            admin_login()


def main():
    startup()


if __name__ == '__main__':
    startup()
\$\endgroup\$
2
\$\begingroup\$

There was quite a lot of code so I decided to just hit some high points.

Pep8:

Python has a strong idea of how the code should be styled, and it is expressed in pep8.

I suggest you get a style/lint checker. I use the pycharm ide which will show you style and compile issues right in the editor.

The primary violation was due to line length issues of strings. The string can (and should) broken into multiple lines. There is a post here on formatting longer strings.

Context managers mean never having to say your closed():

You are using context managers to open your files, but then are explicitly calling close(). if you say this:

with open(file_path, 'r+') as f:

then once the code flow goes out of the context of this block, then the file will automatically be closed. In general you should not close it yourself.

Don't recurse without a good reason:

In several functions in which you are soliciting user input, if the input fails validation, then you recurse to the function to try again. This is unwise and can lead to some hard to find bugs. In general you should just loop around the input and validation code.

Pull common code into a function:

This code:

hashed_data = hashlib.sha224(encoded_data).hexdigest() 

and its surrounding support code appears 4 times. I like to say that once some code appears twice, you should consider consolidating, and when it appears three times, it almost certainly should be consolidated. This hash computation should be in its own function.

Similar Functions:

The functions user_login() and admin_login() are almost identical. You should consider collapsing them into a single function, and add some parameters as needed to allow them do the slightly different things they need to do.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.