samedi 27 juin 2015

Set variable number/depth of if elif else statements

Say I had a list l = [1,2,3,...n] of indeterminate length, and I wanted to perform some if-elif statements according to the list contents, and the length of the list. So if the length was 3, we would have:

p = 2.1

if p >= l[0] and p <= l[1]:
   #do something
elif p >= l[1] and p<= l[2]:
   #do something
elif p >= l[len(l)]:
   #do something
elif p < l[0]:
   #do a final something and quit

So as I have mentioned, the list is of variable length in each case, it could be 32 for example, there is no upper limit in practice. So how do I create a case (a class, function, or whatever the standard method is) where this logic can be applied to any list or length n?

Some pointers would be nice, I haven't progressed to OOP yet, is this my cue to start?

Aucun commentaire:

Enregistrer un commentaire