This is a program that I created using Python. Please help me optimize the code.
import time
import sys
import socket
from time import localtime, strftime
# start up system
def start():
print("Welcome, please choose what to do")
choice = input('register/login ')
if choice == 'register':
register()
elif choice == 'login':
username()
elif choice == 'admin':
username()
else:
problems()
def register():
registeragain = False
global user
global passs
user = input('Please register your username : ')
passs = input('Please register your password : ')
register = input('Do you want to register another account? Y/N : ')
if register == 'Y':
registeragain = True
register()
elif register == 'N':
username()
else:
print("Error")
problems()
# login system
def username():
global admin
admin = 'Max'
userinputname = input('Please enter the username : ')
if userinputname == admin or userinputname == user:
password()
elif userinputname != user:
print('Wrong username, please try again')
username()
else:
problems()
def password():
x = 1
global adminpass
adminpass = 'Luo'
userinputpass = input('Please enter the password : ')
if userinputpass == adminpass:
adminlogin()
elif userinputpass == passs:
loading()
elif userinputpass != passs:
print('Wrong password, please try again')
x += 1
if x > 2:
exit
else:
problems()
# main menu
def loading():
toolbar_width = 40
# setup toolbar
sys.stdout.write("[%s]" % (" " * toolbar_width))
sys.stdout.flush()
# return to start of line, after '['
sys.stdout.write("\b" * (toolbar_width+1))
for i in range(toolbar_width):
time.sleep(0.1) # do real work here
# update the bar
sys.stdout.write("-")
sys.stdout.flush()
sys.stdout.write("]\n") # this ends the progress bar
main()
def main():
time.sleep(3)
print("Welcome to the main screen")
time.sleep(3)
print("Menu: current time, ipchecker, quit")
time.sleep(2)
mainmenu = input('Enter: ')
if mainmenu == 'current time':
telldate()
elif mainmenu == 'ipchecker':
ipchecker()
elif mainmenu == 'quit':
leave()
elif mainmenu == 'cal':
cal()
else:
problems()
# solutions
def problems():
print("We are truly sorry that you have experienced a problem, please contact us at [email protected]")
option = input('Would you like to return to the main menu? Y/N ')
if option == 'Y':
main()
else:
exit()
# admin
def adminlogin():
print("Welcome admin")
mainadmin = input('Enter: ')
if mainadmin == 'current time':
telldate()
elif mainadmin == 'ipchecker':
ipchecker()
elif mainadmin == 'quit':
leave()
elif mainadmin == 'cal':
cal()
else:
problems()
# functions
def telldate():
localtime = time.asctime(time.localtime(time.time()))
print("Local current time :", localtime)
main()
def ipchecker():
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is: " + IPAddr)
main()
def cal():
numx = int(input('Enter the first number: '))
numy = int(input('Enter the second number: '))
op = input('Enter the operation(+, -, *, /): ')
if op == '+':
sum = numx + numy
print('Your sum is ' , sum)
main()
elif op == '-':
diff = numx - numy
print('The difference is ',diff)
main()
elif op == '*':
mult = numx * numy
print('The multiple is ' ,mult)
main()
elif op == '/':
divd = numx / numy
print('The result is ' ,divd)
main()
else:
problems()
def leave():
exit()
start()