1

i'm fairly new to python so i'm sure i'm doing something wrong. i am defining a function which accepts a string variable. i can not be sure exactly what the variable will be, but there a are a 3 values i want to test for and just return a string if are values are found. if those values are not found, i simply want to return 'unknown'. here is my code:

def item_priority(cell_color):
  if cell_color == 'green' or 'yellow':
    return 'low'
  elif cell_color == 'red':
    return 'high'
  else:
    return 'unknown'

so then i try to execute:

>> item_priority('orange')

python returns:

'low'

the result i expected to see would be 'unknown'. even if i test with "item_priority('red')", it still returns 'low'. the only explanations i have found on this site so far involve code that is more complex than mine.

i have tried interchanging the second 'if' with 'elif' but my result is still the same. i'm not sure what i'm doing wrong here. any help is greatly appreciated. thanks!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
user299331
  • 31
  • 1
  • 1
  • 3
  • 3
    Shouldn't it be: `if cell_color == 'green' or cell_color == 'yellow':` – tymeJV Jan 06 '16 at 01:09
  • if (cell_color == 'green' or cell_color == 'yellow'): do_something – z atef Jan 06 '16 at 01:28
  • @tymeJV and NullSoulException, thank you, that does make this example work as expected, but i think i might have over-simplified my example. – user299331 Jan 06 '16 at 01:41
  • Possible duplicate of [if a or b explanation](https://stackoverflow.com/questions/53763935/if-a-or-b-explanation) and/or [Compare multiple variables to the same value in “if” in Python?](https://stackoverflow.com/q/8641008/364696) (the latter isn't exactly what was asked, but covers expanding to testing many values). – ShadowRanger Apr 11 '19 at 16:17
  • Or [Python 'or' in a while loop confusion](https://stackoverflow.com/q/51343863/364696) for someone with the exact same problem. – ShadowRanger Apr 11 '19 at 16:22

4 Answers4

1

'yellow' is always evaluating to True within the if-conditional, therefore that block of code is always being executed with whatever you pass in. You need to add or cell_color == 'yellow' to line 2

aqua
  • 3,269
  • 28
  • 41
  • ok that makes sense. but what if i needed to expand the possibilities which should return 'low'? for example, what would you do if there were like 10 possibilities for 'low'? that would get ugly really quickly. – user299331 Jan 06 '16 at 01:46
  • @user299331 I would put all the possibilities into a list and do `if cell_color in possibilities_for_low_list:`. I realize this response is 7 years overdue, but maybe it will help someone else. – aqua Mar 09 '23 at 05:01
1

The problem is with the line if cell_color == 'green' or 'yellow':. You meant to evaluate if the color is either green or yellow but that is not how or works here

Simply speaking, When you have LEFT or RIGHT like code in python, the LEFT and RIGHT expressions are evaluated first. In your case, this is what happens

  1. LEFT is cell_color == 'green and RIGHT is yellow
  2. When you pass "red" as the color, LEFT evaluates to false
  3. Since the LEFT expression was False, RIGHT is evaluated
  4. Having a string as RIGHT expression, it evaluates to True
  5. False or True evaluates to True and hence the if becomes true and "low" is printed

Important thing to remember is, when you use a non empty string where a boolean is expected, it evaluates to True

>>> bool("")
False
>>> bool("abc")
True

So the faulty line should become if cell_color == 'green' or cell_color == 'yellow':


EDIT: Seeing your comment on another answer, it seems you want to check against multiple colors. In that case, you can use the inbuilt any() function which checks if any of the values of the iterable passed into it evaluates to true.

def item_priority(cell_color):
    low_colors = ["green", "yellow", "..."]
    if any(cell_color == color for color in low_colors):
        return "low"
Teshan Shanuka J
  • 1,448
  • 2
  • 17
  • 31
0

Put your values into an array - then test against that:

validColors = ["red", "black", "blue", "yellow"]
color = "red"

if color in validColors:
    print("Found it")
else:
    print("Not found")

Or, more in tune with your code:

def item_priority(cell_color):
  lowColors = ["green", "yellow"]
  highColors = ["red"]

  if cell_color in lowColors:
    return 'low'
  elif cell_color in highColors:
    return 'high'
  else:
    return 'unknown'

Dictionary approach:

def item_priority(cell_color):
  colors = {}
  colors["high"] = ["red"]
  colors["low"] = ["green", "yellow"]

  if cell_color in colors["low"]:
    return 'low'
  elif cell_color in colors["high"]:
    return 'high'
  else:
    return 'unknown'
tymeJV
  • 103,943
  • 14
  • 161
  • 157
-1
def item_priority(cell_color):
    if cell_color == 'green' or cell_color == 'yellow' :
        return 'low'
    elif cell_color == 'red' :
        return 'high'
    else:
        return 'unknown'
item_priority('Orange')
  • 3
    Thanks for providing code which might help solving the problem, but generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem. – Neuron May 09 '18 at 08:23