0

The code that follows always returns 'orange' I've found other ways to do what I want but I do not understand why this does not work.

color1 = input('select first color')
color2 = input('select second color')  
if 'color1' == 'red' or 'yellow' and 'color2' == 'red' or 'yellow':
    print('orange')
else:
    print('something else')
KEJ
  • 11
  • 2
  • possible duplicate of [Why does \`a == b or c or d\` always evaluate to True?](http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – Jon Clements Jan 29 '15 at 03:04

2 Answers2

1

Please look on the condition statement more deeply, better to make it clearer also,

if 'color1' == 'red' or 'yellow' and 'color2' == 'red' or 'yellow':

We can add parenthesis to make it clearer,

if ('color1' == 'red') or ('yellow') and ('color2' == 'red') or ('yellow')

If we evaluate one group after another we get,

Code ('color1' == 'red')

It will be returning true whenever the input is 'red' and false if it is not 'red'.

Code ('yellow')

No way it evaluate to false. Because in python, non-empty string (text) is considered as / evaluated to true. More over you did it twice!

... etc (we've got the problem).

I think you mean something like this,

if (('color1' == 'red') or ('color1' == 'yellow')) and (('color2' == 'red') or ('color2' == 'yellow')):
Abdillah
  • 982
  • 11
  • 28
0

Actually, you need to write your conditional statement the long way. Like color1 == 'red' or color1 == 'yellow', etc. Otherwise, it is interpreted as checking if the string is not an empty string (the part or 'yellow'). That's not the case because it's the string 'yellow'. Also, be sure to use the variable name (not 'color1', a string).

anto
  • 73
  • 2
  • 5