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?