0

I have problems to set up correctly my if statement.

This is my code:

def task_13():
    Main_meal=['Meat','Cheese','Fish']
    addons=['Potatoes','Rice','Salad']
    my_meal=[(x+y) for x in Main_meal for y in addons if (x+y)!= 'FishRice' and 'CheeseRice']
    print(my_meal)

My question is why Python filter out the 'CheeseRice' when is it stated there but only filter out the 'FishRice' option.

This is my output:

['MeatPotatoes', 'MeatRice', 'MeatSalad', 'CheesePotatoes', 'CheeseRice', 'CheeseSalad', 'FishPotatoes', 'FishSalad']

Thank you for your advice.

Samuel.P
  • 38
  • 11

1 Answers1

1

Here's the official reference on Python operator precedence, note that and is lower precedence than !=, so the != is evaluated first. Also and is a simple operator that takes the booleans on either side and returns a boolean representing their logical AND, it doesn't do what you tried to make it do.

Instead of

if (x+y)!= 'FishRice' and 'CheeseRice'

you need:

if (x+y)!= 'FishRice' and (x+y) != 'CheeseRice'

or alternatively

if (x+y) not in ('FishRice', 'CheeseRice')
Galax
  • 1,441
  • 7
  • 6