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!










share|improve this question


















  • 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















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!










share|improve this question


















  • 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













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!










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












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






share|improve this answer





















  • 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




















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'





share|improve this answer























  • 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




















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')





share|improve this answer

















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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






share|improve this answer





















  • 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

















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






share|improve this answer





















  • 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















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






share|improve this answer












'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







share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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














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'





share|improve this answer























  • 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

















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'





share|improve this answer























  • 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















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'





share|improve this answer














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'






share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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












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')





share|improve this answer

















  • 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















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')





share|improve this answer

















  • 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













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')





share|improve this answer












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')






share|improve this answer












share|improve this answer



share|improve this answer










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














  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values