samedi 24 novembre 2018

Python: "If" function

This is probably really simple but I can't figure it out.

I have a bunch of lists and I want to call certain lists if they are in the range equal to a value x and any number between x-5 and x +5. i.e. x-5,x-4,x-3,x-2,x-1,x,x+1,x+2,x+3,x+4 and x+5.

At the moment I have

if sum(listname[i])==x:

if sum(listname[i])==x-1:

if sum(listname[i])==x-2:

etc

How can I do it so that it is combined in one "if" function.

I've been thinking on the lines of something like:

if sum(listname[i])==x-5>=x>=x+5:

or

if sum(listname[i])==x or x-1 or x-2 ".. etc":

but neither work.

Can anybody shine some light on this?

Aucun commentaire:

Enregistrer un commentaire