I need to figure out what is wrong with this program and correct it so it works. I have been screwing around with it for hours and still cannot figure out what is wrong. I know that I need to change the first line, but I have no idea how to use python, so I have no idea have to recognize a problem and thus cannot diagnose or recover from one either. :( Please help. I know this must be easy for someone out there. :)

The following program asks a user to input a single character. If the user inputs an
“r”, “g”, or “b”, it should print “You have input a color component” and then stop. If the
user inputs any other character it should print “You have not input a color component”,
and then stop.

color = raw_input("Input a color component character: ")
if color == "r":
    print "You have input a color component"
if color == "g":
    print "You have input a color component"
if color == "b":
    print "You have input a color component"
else:
    print "You have not input a color component"

stop.

Admin edit Code section indented for you, please highlight your code next time by yourself and push TAB

Use elif for 'g' and 'b' or use the ìn test for 'rgb' but test also that input is not empty as empty string '' is in every string.

How would you re-write this program then?

@Windiggy..code rewritten using elif.

color = raw_input("Input a color component character: ")
if color == "r":
    print "You have input a color component"
elif color == "g":
    print "You have input a color component"
elif color == "b":
    print "You have input a color component"
else:
    print "You have not input a color component

sorry could'nt edit.my post because of timeout.

color = raw_input ( "Input a color component character: ").strip().lower()
if color in "rgb" and color != "":
    print "You have input a color component"
else:
    print "You have not input a color component"
if color in "rgb" and color != "":

if color is in "rgb" it can not be equal to "" or any other character, so this is redundant.

Sorry, but '' is in every string, and 'rg' is also in 'rgb' so I would formulate this

     if len(color) == 1 and color.lower() in 'rgb':

Putting a strip in input would also make sense.

Alternative would be to use tuple

    if color.lower() in tuple('rgb'):


Doh, of course it is, which is why we always use a list.

testor = "rgb"
for x in ['r', '', 'g']:
    print x,
    if x in list(testor):
        print "is in"
    else:
        print "not"
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.