Home > Net >  Python list how to identify the given number is in the range Challenge Question [closed]
Python list how to identify the given number is in the range Challenge Question [closed]

Time:09-23

In python am having the list like

mylist  = [100  30  400 340, 230, 160,  25]

Given number is : 239

My aim is i have to identify this 239(given number) is in the range of or inbetween of the list each lement with 10 and - 10

Example i have to take the first value from list to verify that 243 is in between 100 10 and 100-10 Not that result false

same way have to compare to 30 10 , 30-10 then again 400 10 and 400-10 same like all

if any one of that inbetween the range i have to return as true if not match with any one then return false

How to do that

Here 239 in between 230 10 and 230-10 so no the result i will get as true

How to do this ??

Thank you in avance

CodePudding user response:

You may iterate on the element of your list, and for each test the expression value-10 < x and x < value 10 that can be written in once in python value-10 < x < value 10

def search_numbers(values, x):
    for value in values:
        if value - 10 < x < value   10:
            print(f"Ok: {value}-10 < {x} < {value} 10")
            return True
    return False

mylist = [100, 30, 400, 340, 230, 160, 25]
print(search_numbers(mylist, 239))  # True
print(search_numbers(mylist, 243))  # False
  • Related