vendredi 8 janvier 2016

python : if statement and multiple variable values [duplicate]

This question already has an answer here:

Good morning,

My problem is the following:

if variable == 'string1':
  do_stuff
elif variable == 'string2' or 'string3':
  do_stuff2

It does not work as it should : do "stuff2" when variable equals "string2" or "string3".

To do that, I have to right the following code:

if variable == 'string1':
  do_stuff
elif variable == 'string2' or variable == 'string3':
  do_stuff2

This code works fine: when variable equals "string2" or "string3", the code excutes stuff2.

I do not understand what is the difference between : elif variable == 'string2' or variable == 'string3' and elif variable == 'string2' or 'string3'.

What does python understand in the second version ?

Thank you for your help,

Pado

Aucun commentaire:

Enregistrer un commentaire