Adding/Removing Colors to Lists I would like the program to ask the user if they want to add a color.  If they say yes, then ask what color to add. Then after that happens, once the program would ask if you want to, run it again. If they say no to add a color, the program ask if you want to remove a color from the list. If they say yes, it would then ask what color to remove, with error handling built in if the color wasn't in that list. Then after that happens once the program would ask if you want to run it again.

Just wanted to point out I do not know a lot about Python, so please don't be harsh, but I can take constructive criticism.

import socket # imports the socket module 
import sys # imports the sys module
import console # imports the console module 

usr = raw_input('What is your name? \n').title() # asks for users name for the hostname information
sname = socket.gethostname() # sets sname to gethostname
print '\nThis program is being run by', usr, 'on machine name', sname # displays host information

mylist = ['Red', 'White', 'Blue', 'Yellow', 'Green', 'Orange'] # sets mylist to colors
templist = list(mylist) # sets templist as mylist
runagain = 'y' # sets runagain to a y values

while runagain == 'y': # for looping
print '\nThe current colors are:', templist # print out list so user knows what colors are already there

clradd = raw_input('\nWould you like to add any colors to the list? y or n\n')

while clradd == 'y':
    colors = raw_input('\nWhat colors would you like to add to the list? Please type one color. \n').title()
    templist.append(colors) # appends color to the list

    print templist # prints out new list
    print '\nAscending and Descending'  # print out ascending and descending lists
    print '-' * 30
    print '\nAscending (high to low): ', sorted(templist, reverse=True)
    print '\nDescending (low to high): ', sorted(templist, reverse=False)

    clrdel = raw_input('\nWould you like to remove any colors from the list? y or n\n')

while clradd == 'n':
    clrdel = raw_input('\nWould you like to remove any colors from the list? y or n\n')

    while clrdel == 'y':
            try:
                removecolor = raw_input('\nWhat colors would you like to remove? ').title() # asks user color to remove
                templist.remove(removecolor) # removes color from the list
            except ValueError:
                print 'Looks like that color was not in the list, try again.'
                continue    

            print 'Updated list\n', templist # prints out updated list

            runagain = raw_input('\nWould you like to run this program again again? y or n')

            if runagain == 'n': # if runagain is n
                console.hide_output() # hide the output 

    while clrdel == 'n':
        runagain = raw_input('\nWould you like to run this program again again? y or n') # asks user to run again

        if runagain == 'n': # if runagain is n
            console.hide_output() # hide the output 
            sys.exit() # stops the program
import socket # imports the socket module 



The comment is completely pointless. But I cannot help it, my eyes naturally read it, my brain processes it, only to realize it says nothing new. Let your code speak for itself, avoid comments as much possible.


print '\nThis program is being run by', usr, 'on machine name', sname



Maybe it's somewhat more readable if you use standard Python formatting like this instead:

print '\nThis program is being run by %s on machine name %s' % (usr, sname)



runagain = 'y'



Instead of setting this to a string, and later using == 'y' to evaluate, it would be better to use proper booleans for this kind of thing. For example:

Similarly, instead of:


clradd = raw_input('\nWould you like to add any colors to the list? y or n\n')

while clradd == 'y':



You could write like this:

clradd = raw_input('\nWould you like to add any colors to the list? y or n\n') == 'y'

while clradd == 'y':


Try-catch block writing style:

try:
    removecolor = raw_input('\nWhat colors would you like to remove? ').title()
    templist.remove(removecolor)
except ValueError:
    print 'Looks like that color was not in the list, try again.'
    continue    


It's better to keep the try block as small as possible. For example, the removecolor assignment should be moved out from it. Also, since you simply want to check if the given color is in the list of colors, it would be cleaner to write that exactly:

removecolor = raw_input('\nWhat colors would you like to remove? ').title()
if removecolor in templist:
    templist.remove(removecolor)
else:
    print 'Looks like that color was not in the list, try again.'
    continue


There are couple of other things too, globally:


Incorrectly indented code
Incorrectly using while instead of a simple if
Duplicated code blocks
sys.exit is a bit harsh way to exit, try to avoid if possible


Suggested implementation

Putting the above suggestions together:

import socket

usr = raw_input('What is your name?\n').title()
sname = socket.gethostname()
print '\nThis program is being run by %s on machine name %s' % (usr, sname)

mylist = ['Red', 'White', 'Blue', 'Yellow', 'Green', 'Orange']
templist = list(mylist)
runagain = True

while runagain:
    print '\nThe current colors are:', templist

    clradd = raw_input('\nWould you like to add any colors to the list? y or n\n') == 'y'

    if clradd:
        colors = raw_input('\nWhat colors would you like to add to the list? Please type one color. \n').title()
        templist.append(colors)

        print templist
        print '\nAscending and Descending'
        print '-' * 30
        print '\nAscending (high to low): ', sorted(templist, reverse=True)
        print '\nDescending (low to high): ', sorted(templist, reverse=False)

    clrdel = raw_input('\nWould you like to remove any colors from the list? y or n\n') == 'y'

    while clrdel:
        removecolor = raw_input('\nWhat colors would you like to remove? ').title()
        if removecolor in templist:
            templist.remove(removecolor)
        else:
            print 'Looks like that color was not in the list, try again.'
            continue

        print 'Updated list\n', templist
        break

    runagain = raw_input('\nWould you like to run this program again again? y or n\n') == 'y'