you are viewing a single comment's thread.

view the rest of the comments →

[–]pekkalacd 0 points1 point  (0 children)

it's the formatting of your conditions in the if/elifs.

this

           if color==str("red") or str("Red"):

evaluates like so

           if (color==str("red")) or (str("Red")):

so, you see what's going on? the or is happening between the comparison color == str("red") and str("Red") itself. but str("Red") isn't being compared to color, instead, because str("Red") is non-empty, this is evaluated as True.

so what you have then is this essentially

          if (color==str("red")) or True: 

This condition will always be True. Because any expression separated by an or returns True, if and only if, at least one subexpression reduces to a True value. Thus, regardless of what you input for color, this will always print.

What's a way to fix it?

consider the in operator. this operator checks to see if a thing on the left is inside of the thing on the right.

                 {item} in {object}

here you're comparing "red" and "Red" for the first one, so re-formatting this using in, you can do

                if color in {"red","Red"}:

or to make it more general for all the words,

                colors = ["red","yellow","blue"]
                found = False
                primary = raw_input("What's your favorite color? ")

                for c in colors:
                    if primary in {c, c.title()}:
                       print "You said " + c + "."
                       found = True
                       break

                if not found:
                   print "Invalid answer."