Reputation: 31
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!
Upvotes: 1
Views: 32014
Reputation: 1548
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
LEFT
is cell_color == 'green
and RIGHT
is yellow
"red"
as the color, LEFT
evaluates to false
LEFT
expression was False, RIGHT
is evaluatedRIGHT
expression, it evaluates to TrueFalse or True
evaluates to True
and hence the if
becomes true and "low"
is printedImportant 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"
Upvotes: 1
Reputation: 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')
Upvotes: -1
Reputation: 104785
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'
Upvotes: 0
Reputation: 3375
'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
Upvotes: 1