Search code examples
python-3.xfunctionexceptionerror-handlingsyntax

Python > Check an int is between two other int


The function in question

def IsInRange(lr,hr,num):
    return lr < num < hr

later, we ask for the input.

lr = floaterror('Provide your low range number, please: ')
hr = floaterror('Provide your high range number, please: ')
num = floaterror('Provide number to range find, please: ')

The assignment asks we say if it's in range. So, I did:

if IsInRange(lr, hr, num) == False:
    print(num, "is not in range.")
elif IsInRange(lr, hr, num) == True:
    print(num, "is in range.")

It works. But let's say,

lr = 1
hr = 1
num = 1

It says 1 isn't in range...I want to create an exception where if this happens it prints "Your check value cannot equal your range." instead of "# is not in range." But I am struggling with this. I cannot change the IsInRange function return per assignment. And I'm sure this is part of it as well as the full code I've written. I have included it below.

def add(num1,num2):
    return num1+num2
def sub(num1,num2):
    return num1-num2
def mlt(num1,num2):
    return num1*num2
def dvd(num1,num2):
    return num1/num2
def IsInRange(lr,hr,num):
    return lr < num < hr

end = True
print("Basic Math Function and In-Range Checker")
while end == True:
    user = input('Please press Enter to continue with a simply mathtastical time, or "q" to quit. ')
    if user == 'q':
        print('You chose to avoid the math! Exiting!')
        break
    else:
        keep_on_rolling = True
        while keep_on_rolling:
        #1st exception to catch invalid entries, that is, not integers.
        #You'll be prompted to enter a correct value.
            def floaterror(one):
                while True:
                    try:
                        return float(input(one))
                    except ValueError:
                        print("Numbers only, please. Try again, with digits!")
        #End 1st catch
            num1 = floaterror('Give us your first number and press enter, please: ')
            num2 = floaterror('Give us your second number and press enter, please: ')
            lr = floaterror('Provide your low range number, please: ')
            hr = floaterror('Provide your high range number, please: ')
            num = floaterror('Provide number to range find, please: ')
            print('The result of', num1, 'added with', num2, 'is', add(num1,num2))
            print('The result of', num1, 'subtracting', num2, 'is', sub(num1,num2))
            print('The result of', num1, 'multiplied by', num2, 'is', mlt(num1,num2))
        #2nd error catch, tryig to destroy the universe by dividing by zero.
            try:
                print('The result of', num1, 'divided by', num2, 'is', (dvd(num1,num2)))
            except ZeroDivisionError:
                print("Uff da. We don't divide by zero in this house.")
        #End 2nd catch.
            else:
                if IsInRange(lr, hr, num) == False:
                    print(num, "is not in range.")
                elif IsInRange(lr, hr, num) == True:
                    print(num, "is in range.")
            break

I didn't keep any of my failed code. But I tried adding an exception under else: which you can't do, I tried if lr == num == hr to print my desired answer. I tried adding an exception above where the function in question comes in. I tried adding an exception after the input. But constantly getting syntax errors or if no errors, it just doesn't seem to appear at all. I also tried defining a new function that uses the inputs and still nothing.


Solution

  • enter code here
    if (not IsInRange(lr, hr, num)) and (not lr == hr and not num == hr):
        print(num, "is not in range.")
    elif IsInRange(lr, hr, num):
        print(num, "is in range.")
    else:
        print("Your check value cannot equal your range.")
    
    if your 
    lr = 1
    num = 1
    hr = 1
     print - "Your check value cannot equal your range."