-1

New to coding. Need a function that checks whether a number is in a given range (inclusive of high and low)

My attempted solution:

def ran_check(num,low,high):
    if num >= low and num <= high:
        print(f'{num} is in between the range {low} and {high}')
    else:
        print(f'{num} is not in between the range {low} and {high}')

The correct solution:

def ran_check(num,low,high):
    if num in range(low,high+1):
        print(f'{num} is in between the range {low} and {high}')
    else:
        print(f'{num} is not in between the range {low} and {high}')

No syntax error on either one, but calling the function using the first returns nothing. Why won't the first one work, aren't they the same thing?

UPDATE The issue was with how I was using Jupyter Notebooks, not the code itself. Thanks for the helpful feedback and tips to write this cleaner!

Codra Kai
  • 1
  • 2
  • What are you expecting it to return precisely? – Ivan Jan 10 '21 at 22:32
  • Both functions work for me, are you sure you aren't doing something else? – Turksarama Jan 10 '21 at 22:33
  • @Turksarama there are some edge cases – M Z Jan 10 '21 at 22:34
  • 2
    0.5 is not in between the range 0 and 1 – Ivan Jan 10 '21 at 22:35
  • expecting: ran_check(6,4,12) to return '6 is in between the range 4 and 12.' Works for the second piece of code but not the first. Using it in a Jupyter notebook. – Codra Kai Jan 10 '21 at 22:36
  • 1
    `ran_check(6,4,12)` does work on both, are you sure you are not overwriting the functions? Notebooks can be evil with their global scope. – Ivan Jan 10 '21 at 22:38
  • 1
    A clearer way to write this would be `if low <= num <= high:`. This would include `float`s and `int`s – Jab Jan 10 '21 at 22:42
  • Ivan seems to be right. This appears to be an issue within Jupyter Notebooks, not with the code itself. – Codra Kai Jan 10 '21 at 22:44
  • 1
    Both functions indeed "return None", by the way – OneCricketeer Jan 10 '21 at 22:47
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. "Doesn't work" is not a problem specification, especially when neither code block does anything. – Prune Jan 10 '21 at 22:55

3 Answers3

0

If num is a floating point number it won't work. Checking if it is in range(low, high + 1) checks to see if it is in [low, low + 1, ..., high], elementwise.

Checking if num >= low and num <= high checks the actual numerical value

M Z
  • 4,571
  • 2
  • 13
  • 27
0

You didn't specify what different behaviour you encounter, but one thing that is different is their relation to floating-point numbers. the range() function creates an array of integers from a low number to a high number, with a step that is an integer as well.

This means that when you check if a number is "in range()" it will check only if it's a part of the integers between those two numbers, whereas a simple range check (>=, <=), will return the correct result for floats as well.

Nimrod Rappaport
  • 134
  • 1
  • 1
  • 12
0

A clearer way to write this would be:

def ran_check(num,low,high):
    is_or_not = 'is' if low <= num <= high else 'is not'
    print(f'{num} {is_or_not} in between the range {low} and {high}')

This will include floats and ints

Jab
  • 26,853
  • 21
  • 75
  • 114