[SOLVED] IsInRange function

Issue

I am currently in a Python class that is online and need to clear up this function thing. I understand you can put functions within functions, but does it even use it. Please look at the code below and help me out. The instructor wants to use the def IsInRange() to see if the numbers are in range, but I could only do it using while statements.

while (True):
    #Get minimum range.
    lowRange = int(input('Enter your Lower range: '))
    #Get maximum range
    highRange = int(input('Enter your Higher range: '))
    #Nested while loop to test ranges.
    while (highRange < lowRange and lowRange >highRange):
        lowRange = int(input('Re-Enter your Lower range: '))
        highRange = int(input('Re-Enter your Higher range: '))
    #Prompts user for first number.
    num1 = int(input('Enter your first number: '))
    #Nested while loop to test num1 for range issues.
    while (num1 > highRange or num1 < lowRange):
        num1 = int(input('Re-Enter your first number in the range:'))
    #Prompts user for second number.
    num2 = int(input('Enter your second number:'))
    while (num2 < lowRange or num2 > highRange):
        num2 = int(input('Re-Enter your second number in the range: '))
       #Run Range function.
        def IsInRange(lr, hr, n1,n2):
            lr = lowRange
            hr = highRange
            n1 = num1
            n2 = num2
            if n1 and n2 < highRange:
                return True
            else:
                return False

        def IsInRange2(lr, hr, n1, n2):
            lr = lowRange
            hr = highRange
            n1 = num1
            n2 = num2
            if n1 and n2 > lowRange:
                return True
            else:
                return False

Solution

You could try something like this

def IsInRange(lr: int, hr: int, n1: int, n2: int):
    #Get min range
    lowRange = int(input('Enter your Lower range: '))
    #Get max range
    highRange = int(input('Enter your Higer range: '))
    #if statement to test ranges.
    if (highRange < lowRange and lowRange >highRange):
        lowRange = int(input('Re-Enter your Lower range: '))
        highRange = int(input('Re-Enter your Higher range: '))
    else:
        pass
    #Prompts user for first number.
    num1 = int(input('Enter your first number: '))
    
    if (num1 > highRange or num1 < lowRange):
        num1 = int(input('Re-Enter your first number within the set range:'))
    #Prompts user for second number.
    num2 = int(input('Enter your second number:'))
    if (num2 < lowRange or num2 > highRange):
        num2 = int(input('Re-Enter your second number within the set range: '))
        
    lr = lowRange
    hr = highRange
    n1 = num1
    n2 = num2
    
    if n1 and n2 < highRange:
        print(True)
        return True
        
    elif n1 and n2 > lowRange:
        print(True)
        return True
    else:
        return False
    
IsInRange(1,10,2,3)

Answered By – Victor A Balderas

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.