I don't know how to make it more neater than this. Can anyone give me advice without changing the way the whole script works?
Also what it does is it has a database of users. You are supposed to login with the correct info, you can exit the program, logout, or register. It's my first real code that I spent some effort on.
Edit: Updated the script.
from tkinter import *
import time, sys, getpass
database = {}
database["admin"] = "123"
database["user"] = "000"
logged = 'false'
listcommands = (
"Logout, Quit, Removal(Admins Only)"
)
def main():
while True:
print("Say 'help' for a list of commands.")
command = input(">>").lower()
if command == 'help':
print(listcommands)
elif command == 'quit':
quitter()
elif command == 'logout':
logout()
else:
print("Command not valid")
def main2():
print("Welcome! Login, register or quit.")
while True:
command2 = input(">>").lower()
if command2 == "login":
login()
elif command2 == "register":
register()
elif command2 == "quit":
quitter()
else:
print("Command not valid")
def login():
print("Please login.")
while True:
username = input("Username: ").lower()
password = getpass.getpass("Password: ")
if username in database.keys():
expected_password = database[username]
if expected_password == password:
print("Welcome " + username)
global logged
logged = 'true'
main()
else:
print("Login is incorrect. Try again.")
else:
print("User does not exit. Try again.")
def register():
print("Register your information")
time.sleep(1)
newuser = input("Username: ").lower()
newpass = getpass.getpass("Password: ")
database[newuser] = newpass
print("Successful!")
main2()
def quitter():
print("Are you sure you want to quit? Y/N?")
while True:
commandquit = input(">>").lower()
if commandquit == "y":
print("Quitting...")
time.sleep(2)
sys.exit(0)
elif commandquit == "n":
print("Returning to main board...")
global logged
if logged == "true":
time.sleep(1)
main()
else:
time.sleep(1)
main2()
else:
print("Command not valid.")
def removal():
print("Work in progress...")
time.sleep(1)
main()
def logout():
print("Are you sure you want to log out? Y/N")
while True:
commandlogout = input(">>").lower()
if commandlogout == "y":
print("Logging out...")
time.sleep(1)
main2()
elif commandlogout == "n":
print("Returning to main board...")
time.sleep(2)
main()
else:
print("Command not valid.")
app = Tk() # Trying to put it into a GUI here. Too tired to do it at this moment
app.title("Python Test")
label = Label(app, text="Python Test Label").pack()
button = Button(app, text="Login",command=lambda: main2()).pack()
app.geometry("500x300")
app.mainloop()