Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with options
#1
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.
.

#!/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")
Reply
#2
(Aug-29-2019, 08:15 PM)raiden Wrote: For instance. Is there a better way to handle this?
You can lowercase userinput before checking conditions, e.g.

elif userinput != ("n") or userinput != ("N") or userinput != ("Y") or userinput != ("y"):
could be rewritten as follows:
elif userinput.lower() not in ('n', 'y'):
Moreover, if you need to make your program case-insensitive to user inputs, you can
lowercase userinput at the beginning. Line #65 could be rewritten:
userinput = raw_input("Is the above information correct? y for yes, n for no: ").lower()
and further if/elif clauses could be simplified.

Finally, since you're using raw_input, you are developing code for Python 2.x. Python 2.x will not be supported starting next year. Move to Python 3.x.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Performance options for sys.stdout.writelines dgrunwal 11 3,108 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  Can argparse support undocumented options? pjfarley3 3 2,184 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  Options for retaining persistent data? hunnimonstr 4 2,953 Feb-14-2018, 07:49 PM
Last Post: hunnimonstr
  Display options - screen tcpip 2 2,833 Feb-06-2018, 02:41 PM
Last Post: tcpip
  Return options Kongurinn 1 3,290 Sep-28-2017, 03:02 PM
Last Post: ichabod801
  Python Launch Options Flexico 6 7,044 Dec-07-2016, 06:58 AM
Last Post: Flexico

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020