mardi 5 janvier 2016

Python 2.7 if / elif statement with or

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!

Aucun commentaire:

Enregistrer un commentaire