Trouble with 'if' statement

dinner = ['chicken','veggies','nuts','drink']
relax = ['chair','computer','desk','wine']
if 'bread' in  dinner or relax:
    print('I relax at dinner eating bread')     
   print('I can not relax at dinner without my bread')    # prints   I relax at dinner eating bread 

… WHY ???

Break up your if statement to see what you’re doing.
You have two expressions

“bread” in dinner == False
relax == True

You aren’t checking if bread is in relax , you’re checking if relax is not empty.

Instead you probably want

if “bread” in dinner or “bread” in relax

1 Like

Precisely what VoodooMan has said. When you put a list in a conditional test by itself Python just checks if it’s empty (False) or if it’s got something in it (True).

Nice…thats why you are the VoodooMan !!

Thanks Alex…nice !!!

you can also add the lists together like this:

if “bread” in (dinner+ relax):

this way temporarily adds the 2 lists together for the check

For code formatting advice, see Discourse Guide: Code Formatting - Meta - Stonehearth Discourse

1 Like