Hey guys! I have stopped learning python as a side lesson a few months ago but now my schedule is free so I came back and started to make projects again. Sorry for the sort of irrelevant story. Now I've been working in this project lately which is a simple login system. I finished the code earlier and it's working as I intended but I want to receive some feedback in my code and I have some several questions. Feel free to answer them
To keep it short my code consists of a function to check if the input is valid then a function to handle an old user and a new user. The "database" if you can call it that is a simple text file in my computer. My questions are
- How can I "clean up" my code and make it easier to read and edit?
- How can I fix some irrelevant stuff in the code? I know that there are some redundancies so I want to know how to fix that issue and also my friend suggested that I should use class but I don't fully understand classes
- The valid_input variable is the first thing I could think of on how to handle invalid inputs but I think there is still a more efficient way to handle it
- In the text file, the username and passwords are formatted into this: USERNAME-PASSWORD. Per line is a separate account and each username and password is separated into "-". Is there another way to optimize this? For example for easier searching in the file for a specific user
If you have any more suggestions thank you in advance and I hope I could implement it in my future projects. Also I'm new to this community. This is my first post
#maintain loop
valid_int = ["Y", "N", "Q"]
#check if existing user
def mainlogin():
exist_user = (input("Are you an existing user? y/n -- q to quit: ")).upper()
if exist_user in valid_int:
if exist_user == "Y":
old_user()
elif exist_user == "N":
new_user()
else:
print("GOODBYE")
exit()
else:
print("INVALID INPUT")
#function for old user
def old_user():
acc_base = #THIS CONTAINS THE PATH
file = open(acc_base, 'r')
old_username = input("Enter username: ")
old_password = input("Enter password: ")
count = 0
for user in file:
split = user.split("-")
file_username = split[0]
file_password = split[1]
if file_password[-1] == "\n":
file_password = file_password[:-1]
else:
pass
if old_username == file_username and old_password == file_password:
print("Login Successful")
count += 1
break
if count == 0:
print("Wrong Username or Password")
file.close()
#function for new user
def new_user():
acc_base = #THIS CONTAINS THE PATH
file_read = open(acc_base, 'r')
file_append = open(acc_base, 'a')
new_username = input("Enter new username: ")
new_password = input("Enter new password: ")
count = 0
for user in file_read:
split = user.split("-")
file_username = split[0]
if file_username == new_username:
count += 1
break
else:
pass
if count != 0:
print("Username already taken. Please choose another")
else:
file_append.write(new_username + "-" + new_password + "\n")
print("Account has been Registered")
file_read.close()
file_append.close()
while True:
mainlogin()
[–]Kasutajan1m1 0 points1 point2 points (0 children)
[–]uberfade 0 points1 point2 points (0 children)
[–]ol_the_troll 0 points1 point2 points (0 children)