create a function of a set of integers and a value for which you need to find the nearest one.
def nearest(numbers, value): small = [] large = [] for i in numbers: if i < value: small.append(i) else: large.append(i) if large and small: return max(small) if value - max(small) <= min(large) - value else min(large) return max(small) or min(large) def main(): numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] value = 6 print(nearest(numbers, value)) main()