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_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name)
x = input()
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()
else:
print('Wrong combinations\nAdmin name must be 6 characters long and Password must be 8 characters long')
startup()
home()

def home():
print('Press "A" to Login\nPress "B" to create a new user')
x = input().lower()
if x == 'a':
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:
x = input()
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')
w_user.close()
else:
print('Wrong combinations\nAdmin name must be 6 characters long and Password must be 8 characters long')
create_new_user()

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':
elif x == 'b':
else:
print('Invalid option selected\nPlease press "A" or "B"')

x = input()
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:
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('Welcome ' + x)
f.close()
else:

file_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name)
x = input()
y = input()
with open(file_path, 'r+') as f:
raw_input = x + ':' + y
encoded_input = raw_input.encode('utf-8')
hasshed_input = hashlib.sha224(encoded_input).hexdigest()
print('Welcome ' + x)
f.close()
else:

def main():
startup()

if __name__ == '__main__':
startup()


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.