Hello, Im new to python programing. I wrote the folowing program last night.
It just gets basic user information and writes it to a text file.
My question is , Is there a more optimized or better way to write the code?
better way to handel the error's ?
any idea's or help or sugestions would be helpful. Not looking to have someone tell me what to write just someone to point me in the right direction to better write the code. Thank you in advance.
.
For instance. Is there a better way to handle this?
It just gets basic user information and writes it to a text file.
My question is , Is there a more optimized or better way to write the code?
better way to handel the error's ?
any idea's or help or sugestions would be helpful. Not looking to have someone tell me what to write just someone to point me in the right direction to better write the code. Thank you in advance.
.
#!/usr/bin/python #Simple Script for User Database, First Name, Last Name, Address, Email, Phone Number #need better error handeling import sys import os print ("\n") print ("\033[1;32m" + "WELCOME TO THE ARIES USER DATABASE") print ("Type -h for the help screen") print ("\n") def mainmenu(): commandprompt = raw_input(":") # Open and Display the Entire Database if commandprompt == ('-d'): database = open("userInformation.txt", "r") print (database.read()) database.close() mainmenu() # Call the Function to Enter New User Information elif commandprompt == ('-n'): userInfo() # help menu elif commandprompt == ('-h'): print ("-d = Show Entire Database") print ("-x = Delete Database") print ("-h = Show This Help") print ("-n = Add User To Database") print ("-q = quit AUD") mainmenu() #Quit the Program elif commandprompt == ('-q'): sys.exit() elif commandprompt == ('-x'): xprompt = raw_input("Are you sure you want to remove the database ?: ") if xprompt == ("y") or xprompt == ("Y"): os.remove("userInformation.txt") print ("Database Removed!") mainmenu() elif xprompt == ("n") or xprompt == ("N"): print ("Database was not removed!") mainmenu() elif xprompt != ('n') or xprompt !=('N') or xprompt != ('y') or xprompt != ('Y'): print ("Invalid command") mainmenu() # error handeling there is prob a better way elif commandprompt != ("-d", "-h", "-n", "-q"): # must do this line for each used command. IE commandprompt != () print ("invalid command. Please try again or type -h for help") mainmenu() def userInfo(): fname = raw_input("First Name: ") lname = raw_input("Last Name: ") address = raw_input("Address: ") city = raw_input("City: ") state = raw_input("State: ") zipcode = raw_input("Zipcode: ") email = raw_input("Email: ") phonenumber = raw_input("Phone Number: ") userInfoList = (fname, lname, address, city, state, zipcode, email, phonenumber) print (userInfoList[0:7]) #must be a better way to do this. maybe a while loop ? # if anything other than n N y Y are entered the entire func userInfo is called again. # would be nice to only display the correct information line when wrong key is entered not the entire function userinput = raw_input("Is the above information correct? y for yes, n for no: ") if userinput == ("y") or userinput == ("Y"): userInformation = open("userInformation.txt", "a") for item in userInfoList: userInformation.writelines("%s\n" % item) userInformation.close() print ("User Information Added") mainmenu() elif userinput == ("n") or userinput == ("N"): userInfo() elif userinput != ("n") or userinput != ("N") or userinput != ("Y") or userinput != ("y"): print ("that is not a valid command must be y or n") userInfo() mainmenu()
For instance. Is there a better way to handle this?
elif userinput != ("n") or userinput != ("N") or userinput != ("Y") or userinput != ("y"): print ("that is not a valid command must be y or n")