Im trying to write a code for a colour picker, so a user can pick 4 different colours and then it would return them so i can use them in a different par tof the program, but its not working properly and i can work out why. any ideas?

def colourpicker():
    print "pick four different colours from; red, green, blue, yellow, orange, pink, brown"
    colourList = ["red", "green", "blue", "yellow", "orange", "pink", "brown"]
    coloursPicked = []
    while True:
        colour1 = raw_input("pick first colour: ")
        if colour1 in colourList:
            break
        colour1 = raw_input("not in list, pick agin: ")
    coloursPicked.append(colour1)
    return coloursPicked
    while True:
        colour2 = raw_input("pick second colour: ")
        if colour2 in coloursPicked:
            break
        colour2 = raw_input("colour already picked, pick again: ")
        if colour2 in colourList:
            break
        colour2 = raw_input("not in list, pick agin: ")
    coloursPicked.append(colour2)
    return coloursPicked
    while True:
        colour3 = raw_input("pick third colour: ")
        if colour3 in coloursPicked:
            break
        colour3 = raw_input("colour already picked, pick again: ")
        if colour3 in colourList:
            break
        colour3 = raw_input("not in list, pick agin: ")
    coloursPicked.append(colour3)
    return coloursPicked
    while True:
        colour4 = raw_input("pick fouth colour: ")
        if colour4 in coloursPicked:
            break
        colour4 = raw_input("colour already picked, pick again: ")
        if colour4 in colourList:
            break
        colour4 = raw_input("not in list, pick agin: ")
    coloursPicked.append(colour4)
    return coloursPicked

i think some of the loops might not be working how i want them too, but i cant seem to work it out.

Add some print statements so you know what is going on.

def colourpicker():
    print "pick four different colours from; red, green, blue, yellow, orange, pink, brown"
    colourList = ["red", "green", "blue", "yellow", "orange", "pink", "brown"]
    coloursPicked = []
    while True:
        colour1 = raw_input("pick first colour: ")
        print "colour1 picked =", colour1

        if colour1 in colourList:

            print "colour1 in colourList"
            break
        colour1 = raw_input("not in list, pick agin: ")
    coloursPicked.append(colour1)

    print "returning", coloursPicked
    return coloursPicked

    ##  etc. 
    ## consider using one function and calling it 4 times for the colors

I tried to make one that works as a smaller version. it seems to work a bit better, but i dont know how i can make it take a wrong answer, tell you its wrong, then accept the next correct answer as a choice.

def colourPicker():
    print "pick four different colours from; red, green, blue, yellow, orange, pink, brown"
    colourList = ["red", "green", "blue", "yellow", "orange", "pink", "brown"]
    chosenColours = []
    for i in range(4):
        while True:
            colourPicked = raw_input("pick a colour: ")
            if colourPicked in colourList:
                break
            colourPicked = raw_input("not in list, pick again: ")
        chosenColours.append(colourPicked)
    print chosenColours

what this does is when it takes a wrong answer, if you put a correct one in next it doesnt take it as an aswer, it just goes back to the start of the loop. is there anyway to get it to get the second answer and accept it if its right?

See if this help you.

def colourPicker():
    print "pick four different colours from; red, green, blue, yellow, orange, pink, brown"
    colourList = ["red", "green", "blue", "yellow", "orange", "pink", "brown"]
    chosenColours = []
    count = 0
    while True:            
        colourPicked = raw_input("pick a colour: ")
        if colourPicked in colourList:
            chosenColours.append(colourPicked)
            if len(chosenColours) == 4:  #If length of list is 4 do next line
                break  #For test we use break this is what you will do "return chosenColours" 
            else:
                count +=1
                print 'Color %d is picked and it is %s ' % (count,chosenColours)           
        else:
            print 'Not in list'       
    print chosenColours    

colourPicker()
'''-->output
pick four different colours from; red, green, blue, yellow, orange, pink, brown
pick a colour: ghgh
Not in list
pick a colour: red
Color 1 is picked and it is ['red'] 
pick a colour: green
Color 2 is picked and it is ['red', 'green'] 
pick a colour: pink
Color 3 is picked and it is ['red', 'green', 'pink'] 
pick a colour: brown
['red', 'green', 'pink', 'brown']
'''

This is a more or less standard way of entering data. Use a function, and return from the function only if a correct value is entered.

def colour_picker():
    colourList = ["red", "green", "blue", "yellow", "orange", "pink", "brown"]
    print "pick four different colours from;",
    print ",".join(colourList)

    while True:
            colourPicked = raw_input("pick a colour: ")
            if colourPicked in colourList:
                return colourPicked
            print "not in list, pick again: "


chosenColours = []
for x in range(4):
        colour_returned = colour_picker()
        chosenColours.append(colour_returned)
print chosenColours
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.