-1

I seem to be having an issue with the and and in operator when using dictionaries in Python. I have a dictionary listed with different pieces of clothing inside it, and I have a separate function that deletes these items from the dictionary.

clothes = {"socks": 1, "shoes": 2}

def status():
    if "socks" and "shoes" in clothes:
        print("You are wearing socks and shoes.")
    elif "socks" in clothes:
        print("You are wearing only socks.")
    elif "shoes" in clothes:
        print("You are wearing only shoes.")
    else:
        print("You are not wearing socks or shoes.")

If I have both the socks or shoes variable in the clothes dictionary, it will print You are wearing socks and shoes.. But if I remove either one, it still fulfills the first if function as if both are true, which isn't the case. Only when I remove both do I get a different output, and that jumps to the else function.

I am assuming it's an issue with the in operator, or I don't properly understand what the and operator does, but from reading the documentation it Returns True if both statements are true, so I'm at a bit of a loss.

I'm sure there are other ways to go about this, but I'm not quite sure why it doesn't work here. Any clues?

Elevendy
  • 1
  • 1
  • How do you remove them exactly ? did you try "socks" in clothes and "shoes" in clothes – Berkay Berabi Jul 16 '20 at 17:02
  • @berkayberabi I used `del clothes["socks"]` and `del clothes["shoes"]` – Elevendy Jul 16 '20 at 17:04
  • 1
    "from reading the documentation it Returns True if both statements are true". In your own words, what do you think the two statements are, in the line where you use `and`? Answer: no, they are **not** `"socks" in clothes` and `"shoes" in clothes`. They are `"socks"` and `"shoes" in clothes`. "But if I remove either one, it still fulfills" Not so; it will still pass if you remove the `socks`, but not if you leave the `socks` and remove the `shoes`. – Karl Knechtel Jul 16 '20 at 17:10
  • Also, `if` is not a function, and neither is the expression you write after the word `if`. The term you're looking for is *condition*. – Karl Knechtel Jul 16 '20 at 17:10

3 Answers3

1

In the first line, you forget to check if "socks" are in clothes.

if "socks" in clothes and "shoes" in clothes:
        print("You are wearing socks and shoes.")
Bob
  • 1,004
  • 9
  • 12
1

You have to call clothes when you want to check whether any item exists. It does not work if you pass 2 strings and then check in a variable. You have to check every string in a variable.

if "socks" in clothes and "shoes" in clothes:

The AND operator(logical gate) multiply values of 2 things. It will return True only if both values are True. When you are using a string without checking it exists in a variable, it will return True because it checks whether the string is True - whether it has value.

Aleksander Ikleiw
  • 2,549
  • 1
  • 8
  • 26
0

Due to operator precedence, the statement

if "socks" and "shoes" in clothes:

is interpreted as if it were

if "socks" and ("shoes" in clothes):

A non-empty string is always truthy, so this is essentially equivalent to

if "shoes" in clothes:

Logical operators are not automatically distributed, you need to specify each test and combine them

if "socks" in cloths and "shoes" in clothes:
Barmar
  • 741,623
  • 53
  • 500
  • 612