Reputation: 23
I've been given a project and although I've managed to get it to work, part of it looks very messy and I can't think of what I'm sure is a simple fix to it. My knowledge of programming is minimal so most Google searches for it have confused me and I don't really know how to define what I want. Anyone here is the code:
(if x in range(25,76) or x in range(125,176) or x in range(225,276) or
x in range(325,376) or x in range(425,476) or x in range(525,576) or
x in range(625,676) or x in range(725,776)):
I think you can see what I'm trying to do but I've had to list it as separate ranges each time, help will be much appreciated!
Upvotes: 2
Views: 3660
Reputation: 122032
One option for shortening a lot of or
s is to use any
, something like:
if any(x in range(start, end) for start, end in [(25, 76), (125, 176), ...]):
or just use arithmetic, if the values will always be integers:
if any(start <= x < end ...):
Alternatively, as you seem to have a regular pattern of ranges (i.e. between 25
and 76
in every hundred), you could use modulo (%
) to strip away the hundreds:
if x % 100 in range(25, 76):
and add in x < 776
if that's a fixed upper limit
Upvotes: 7