You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中IsInRange函数特殊场景(lr=hr=num)处理求助

解决方案:不修改IsInRange函数实现特殊场景提示

核心思路:在调用IsInRange进行范围判断前,优先检查lr、hr、num三者是否完全相等的特殊场景,单独处理该场景的输出;其余情况再依据原函数的返回结果输出对应提示。

修改后的关键逻辑

将原代码中范围判断的else块替换为以下逻辑:

# 先处理三者相等的特殊场景
if lr == hr == num:
    print("Your check value cannot equal your range.")
else:
    if IsInRange(lr, hr, num):
        print(num, "is in range.")
    else:
        print(num, "is not in range.")

完整修改后的代码

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, trying 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.
            # 替换原有的范围判断逻辑,优先处理三者相等的场景
            if lr == hr == num:
                print("Your check value cannot equal your range.")
            else:
                if IsInRange(lr, hr, num):
                    print(num, "is in range.")
                else:
                    print(num, "is not in range.")
            break

修改说明

  • 新增lr == hr == num的条件判断,优先级高于原函数的范围检查,确保特殊场景优先触发指定提示
  • 原IsInRange函数的逻辑完全保留,符合“不能修改返回逻辑”的要求
  • 其余场景仍按照原函数的返回结果输出正常的范围提示

内容的提问来源于stack exchange,提问作者Spec _

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 08:52:34