Python 2.7 if / elif statement with or
up vote
1
down vote
favorite
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!
if-statement
add a comment |
up vote
1
down vote
favorite
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!
if-statement
1
Shouldn't it be:if cell_color == 'green' or cell_color == 'yellow':
– tymeJV
Jan 6 '16 at 1:09
if (cell_color == 'green' or cell_color == 'yellow'): do_something
– zee
Jan 6 '16 at 1: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 6 '16 at 1:41
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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!
if-statement
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!
if-statement
if-statement
asked Jan 6 '16 at 1:01
user299331
1313
1313
1
Shouldn't it be:if cell_color == 'green' or cell_color == 'yellow':
– tymeJV
Jan 6 '16 at 1:09
if (cell_color == 'green' or cell_color == 'yellow'): do_something
– zee
Jan 6 '16 at 1: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 6 '16 at 1:41
add a comment |
1
Shouldn't it be:if cell_color == 'green' or cell_color == 'yellow':
– tymeJV
Jan 6 '16 at 1:09
if (cell_color == 'green' or cell_color == 'yellow'): do_something
– zee
Jan 6 '16 at 1: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 6 '16 at 1:41
1
1
Shouldn't it be:
if cell_color == 'green' or cell_color == 'yellow':
– tymeJV
Jan 6 '16 at 1:09
Shouldn't it be:
if cell_color == 'green' or cell_color == 'yellow':
– tymeJV
Jan 6 '16 at 1:09
if (cell_color == 'green' or cell_color == 'yellow'): do_something
– zee
Jan 6 '16 at 1:28
if (cell_color == 'green' or cell_color == 'yellow'): do_something
– zee
Jan 6 '16 at 1: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 6 '16 at 1:41
@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 6 '16 at 1:41
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
'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
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 6 '16 at 1:46
add a comment |
up vote
0
down vote
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'
Note: If there are a lot of categories, a dictionary is a better choice.
– Karoly Horvath
Jan 6 '16 at 13:48
@KarolyHorvath -- Added a dictionary approach - is that what you meant? (pretty new to Python as well)
– tymeJV
Jan 6 '16 at 13:52
Nope. Just a color -> category lookup. And looks like you aren't familiar with dictionary literals. Look up the syntax.
– Karoly Horvath
Jan 6 '16 at 14:56
add a comment |
up vote
0
down vote
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')
2
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.
– Lonely Neuron
May 9 at 8:23
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
'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
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 6 '16 at 1:46
add a comment |
up vote
0
down vote
'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
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 6 '16 at 1:46
add a comment |
up vote
0
down vote
up vote
0
down vote
'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
'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
answered Jan 6 '16 at 1:24
aqua
2,8412036
2,8412036
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 6 '16 at 1:46
add a comment |
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 6 '16 at 1:46
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 6 '16 at 1:46
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 6 '16 at 1:46
add a comment |
up vote
0
down vote
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'
Note: If there are a lot of categories, a dictionary is a better choice.
– Karoly Horvath
Jan 6 '16 at 13:48
@KarolyHorvath -- Added a dictionary approach - is that what you meant? (pretty new to Python as well)
– tymeJV
Jan 6 '16 at 13:52
Nope. Just a color -> category lookup. And looks like you aren't familiar with dictionary literals. Look up the syntax.
– Karoly Horvath
Jan 6 '16 at 14:56
add a comment |
up vote
0
down vote
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'
Note: If there are a lot of categories, a dictionary is a better choice.
– Karoly Horvath
Jan 6 '16 at 13:48
@KarolyHorvath -- Added a dictionary approach - is that what you meant? (pretty new to Python as well)
– tymeJV
Jan 6 '16 at 13:52
Nope. Just a color -> category lookup. And looks like you aren't familiar with dictionary literals. Look up the syntax.
– Karoly Horvath
Jan 6 '16 at 14:56
add a comment |
up vote
0
down vote
up vote
0
down vote
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'
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'
edited Jan 6 '16 at 13:52
answered Jan 6 '16 at 13:46
tymeJV
90.8k12117135
90.8k12117135
Note: If there are a lot of categories, a dictionary is a better choice.
– Karoly Horvath
Jan 6 '16 at 13:48
@KarolyHorvath -- Added a dictionary approach - is that what you meant? (pretty new to Python as well)
– tymeJV
Jan 6 '16 at 13:52
Nope. Just a color -> category lookup. And looks like you aren't familiar with dictionary literals. Look up the syntax.
– Karoly Horvath
Jan 6 '16 at 14:56
add a comment |
Note: If there are a lot of categories, a dictionary is a better choice.
– Karoly Horvath
Jan 6 '16 at 13:48
@KarolyHorvath -- Added a dictionary approach - is that what you meant? (pretty new to Python as well)
– tymeJV
Jan 6 '16 at 13:52
Nope. Just a color -> category lookup. And looks like you aren't familiar with dictionary literals. Look up the syntax.
– Karoly Horvath
Jan 6 '16 at 14:56
Note: If there are a lot of categories, a dictionary is a better choice.
– Karoly Horvath
Jan 6 '16 at 13:48
Note: If there are a lot of categories, a dictionary is a better choice.
– Karoly Horvath
Jan 6 '16 at 13:48
@KarolyHorvath -- Added a dictionary approach - is that what you meant? (pretty new to Python as well)
– tymeJV
Jan 6 '16 at 13:52
@KarolyHorvath -- Added a dictionary approach - is that what you meant? (pretty new to Python as well)
– tymeJV
Jan 6 '16 at 13:52
Nope. Just a color -> category lookup. And looks like you aren't familiar with dictionary literals. Look up the syntax.
– Karoly Horvath
Jan 6 '16 at 14:56
Nope. Just a color -> category lookup. And looks like you aren't familiar with dictionary literals. Look up the syntax.
– Karoly Horvath
Jan 6 '16 at 14:56
add a comment |
up vote
0
down vote
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')
2
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.
– Lonely Neuron
May 9 at 8:23
add a comment |
up vote
0
down vote
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')
2
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.
– Lonely Neuron
May 9 at 8:23
add a comment |
up vote
0
down vote
up vote
0
down vote
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')
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')
answered May 9 at 7:39
Nikita Doshi
11
11
2
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.
– Lonely Neuron
May 9 at 8:23
add a comment |
2
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.
– Lonely Neuron
May 9 at 8:23
2
2
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.
– Lonely Neuron
May 9 at 8:23
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.
– Lonely Neuron
May 9 at 8:23
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34623984%2fpython-2-7-if-elif-statement-with-or%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Shouldn't it be:
if cell_color == 'green' or cell_color == 'yellow':
– tymeJV
Jan 6 '16 at 1:09
if (cell_color == 'green' or cell_color == 'yellow'): do_something
– zee
Jan 6 '16 at 1: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 6 '16 at 1:41