yoyo
yoyo

Reputation: 25

Checking whether a list value is within a range

I have created a list of numbers in fav_numbers. The user will be prompted to enter 2 values: starting range and ending range. The program will check if the list of numbers will fall into the range that the user has inputted. If the numbers in the list fall within the range, the list will be printed out. The thing is I'm not sure how to check if the individual numbers in the list are within the range. I am a beginner to python so what would be the most straightforward way to do this, preferably using loops?

start_range = int(input("Enter the starting number: "))
end_range = int(input("Enter the ending number: "))

#find favourite numbers
def find_favourite_numbers(start_range,end_range):
    fav_numbers = [70, 105, 140]
    for start_range and end_range in range(fav_numbers):
    if fav_numbers[0,1,2] >= start_range and fav_number <= end_range:
        print(fav_numbers)
    else:
        print("favourite numbers not in range")

    print(fav_numbers) 

    return fav_numbers


#Do not remove the next line
find_favourite_numbers(start_range,end_range)

Upvotes: 1

Views: 1085

Answers (3)

Tomerikoo
Tomerikoo

Reputation: 19430

The way I see it you have a few options in descending order of efficiency:

Assuming the list is sorted as the example:

If the list is sorted, it is enough to just check the edges:

if start_range <= fav_numbers[0] and fav_numbers[-1] <= end_range:
    print("numbers are in range")

The general case (list is not sorted):

Obviously sorting the list will bring you back to the previous section.

Alternatively, you can just do:

if start_range <= min(fav_numbers) and max(fav_numbers) <= end_range:
    print("numbers are in range")

Lastly, the optimized version of a regular loop will be to use the built-in any/all functions:

if all(start_range <= num <= end_range for num in fav_numbers):
    print("numbers are in range")

The nice thing with using these functions is short-circuiting. This basically means that not all values will necessarily be checked. In the example above (using all), if a number is out of range, the loop will break, or - short-circuit. This means that the above single line of all is equivalent to the more messy:

for num in fav_numbers:
    if not start_range <= num <= end_range: # equivalent to if num < start_range or num > end_range
        return False
return True

Upvotes: 1

alani
alani

Reputation: 13079

One suggestion would be to make a separate function which tests whether all the numbers are in range, and returns a True or False value, as this makes it a bit tidier. Inside the fav_numbers_are_in_range function is the loop that you were asking about, but if it finds a number out of range, then it just returns from the function so it does not execute the whole loop.

start_range = int(input("Enter the starting number: "))
end_range = int(input("Enter the ending number: "))


# test if favourite numbers are in rage
def fav_numbers_are_in_range(fav_numbers, start_range, end_range):
    for number in fav_numbers:
        if number < start_range or number > end_range:
            return False
    return True


#find favourite numbers
def find_favourite_numbers(start_range,end_range):
    fav_numbers = [70, 105, 140]
    if fav_numbers_are_in_range(fav_numbers, start_range, end_range):
        print("fav numbers are in range ", fav_numbers)
        return fav_numbers
    else:
        print("not all favourite numbers are in range")
        return None


find_favourite_numbers(start_range,end_range)

Upvotes: 0

Nico M&#252;ller
Nico M&#252;ller

Reputation: 1874

You could do the below, loop through all the fav_numbers, if one is not in range it will output that the numbers are not all in range and return False , otherwise if the loop completes it will returns the numbers

start_range = int(input("Enter the starting number: "))
end_range = int(input("Enter the ending number: "))

#find favourite numbers
def find_favourite_numbers(start_range,end_range):
    fav_numbers = [70, 105, 140]
    for number in fav_numbers:
        if number < start_range or number > end_range:
            print("Not all favourite numbers in range")
            return False
    print("All favourite numbers in range: " + str(fav_numbers)) 

    return fav_numbers


#Do not remove the next line
find_favourite_numbers(start_range,end_range)

Upvotes: 0

Related Questions