0

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

Edited by pyTony: Code tag advise

5
Contributors
8
Replies
12
Views
5 Years
Discussion Span
Last Post by woooee
0

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.

0

@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
0

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"
0
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.

Edited by woooee

0

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'):


Edited by pyTony

0

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"

Edited by woooee

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.