0

Sorry for posting such a naive question, but I'm just not able to figure this out. I have written following conditional statements:

if taxon == "Bracelets":
    catId = "178785"
elif taxon == "Kids Earrings" or "Earrings":
    catId = "177591"
elif taxon == "Mangalsutras":
    catId = "177595"
elif taxon == "Necklaces" or "Necklace Sets":
    catId = "177597"
elif taxon == "Kids Pendants" or "Pendants":
    catId = "177592"
elif taxon == "Pendant Sets":
    catId = "177593"
elif taxon == "Anklets":
    catId = "178788"
elif taxon == "Toe Rings":
    catId = "178787"
elif taxon == "Rings":
    catId = "177590"
else:
    print "no match\n"

But no matter what the value of taxon is, its always falling in the second condition i.e.

elif taxon == "Kids Earrings" or "Earrings":
    catId = "177591"

and therefore, the value of catId remains 177591.

nish
  • 6,952
  • 18
  • 74
  • 128
  • `elif taxon == "Kids Earrings" or taxon == "Earrings":` otherwise the 2nd condition will always be true – reto Dec 17 '13 at 12:14
  • @MartijnPieters : True. But, it was tough to find it as the name of the question does not suggest anything abt the content – nish Dec 17 '13 at 12:22
  • That's alright, this is an issue many beginners run into without realizing what is going wrong. – Martijn Pieters Dec 17 '13 at 12:23

7 Answers7

13

This should be

elif taxon == "Kids Earrings" or taxon == "Earrings":

Your original code tests the truth value of "Earrings" rather than whether taxon has the value "Earrings"

>>> bool("Earrings")
True

A better way to structure this is with a dictionary:

id_map = {}
id_map["Bracelets"] = "178785"
id_map["Earrings"] = "177591"
id_map["Kids Earrings"] = "177591"
# etc

then later you can do

id_map[taxon]

This also lends itself better to storage in a configuration file or database, to avoid hard-coding the values in your Python code.

YXD
  • 31,741
  • 15
  • 75
  • 115
7

Others have already given the syntactical answer to your problem.

My answer is to change this ugly code to use a dictionary lookup. For example:

taxes = {"Bracelets": 178785, "Necklaces": 177597, "Necklace Sets": 177597}
#etc

Then you use

catId = taxes[taxon]
wim
  • 338,267
  • 99
  • 616
  • 750
3

Use this idiom:

elif taxon in ("Kids Earrings", "Earrings"):
David Zwicker
  • 23,581
  • 6
  • 62
  • 77
1

The problem is that it's always true since it evaluates for Boolean True, it checks if the string is empty.

Change to:

if taxon == "Bracelets":
    catId = "178785"
elif taxon == "Kids Earrings" or taxon == "Earrings":
    catId = "177591"
elif taxon == "Mangalsutras":
    catId = "177595"
elif taxon == "Necklaces" or taxon == "Necklace Sets":
    catId = "177597"
elif taxon == "Kids Pendants" or taxon == "Pendants":
    catId = "177592"
elif taxon == "Pendant Sets":
    catId = "177593"
elif taxon == "Anklets":
    catId = "178788"
elif taxon == "Toe Rings":
    catId = "178787"
elif taxon == "Rings":
    catId = "177590"
else:
    print "no match\n

In a personal note i would use python dict is really good instead of if else:

options = {"option1": "value1", "option2": "value2".....}
Kobi K
  • 7,743
  • 6
  • 42
  • 86
1

This condition:

taxon == "Kids Earrings" or "Earrings"

looks like

(taxon == "Kids Earrings") or "Earrings"

which is always true because "Earrings" evaluates true (is a non-empty string).

You'd like to do:

taxon in ("Earrings, "Kids Earrings")

or just write several conditions:

taxon == "Earrings" or taxon == "Kids Earrings"

or perhaps:

taxon.endswith("Earrings")

Kos
  • 70,399
  • 25
  • 169
  • 233
1

Use

elif taxon in ("Kids Earrings", "Earrings"):
Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
0

the second condition isn't checked against a variable. like this it makes no sense! try it this way:

...
elif taxon == "Kids Earrings" or taxon == "Earrings":
    catId = "177591"
...
PrR3
  • 1,250
  • 1
  • 18
  • 26