Python 这些函数看起来像';d做同样的事情,但一个人能行,一个人不行';T为什么?

Python 这些函数看起来像';d做同样的事情,但一个人能行,一个人不行';T为什么?,python,function,Python,Function,新的编码。需要一个函数来检查数字是否在给定范围内(包括高和低) 我尝试的解决方案: 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 {hi

新的编码。需要一个函数来检查数字是否在给定范围内(包括高和低)

我尝试的解决方案:

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}')
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}')
这两个函数都没有语法错误,但是使用第一个函数调用函数不会返回任何结果。为什么第一个不起作用,它们不是一样吗


更新问题在于我如何使用Jupyter笔记本,而不是代码本身。感谢您提供有用的反馈和提示来编写此清洁剂

如果
num
是一个浮点数,它将不起作用。检查它是否在
范围内(低,高+1)
检查它是否在
[低,低+1,…,高]
,元素方面


检查
num>=low
num时,您没有指定遇到的不同行为,但有一点不同,那就是它们与浮点数的关系。函数的作用是:创建一个从低位到高位的整数数组,步长也是整数


这意味着,当您检查一个数字是否“在range()”时,它只会检查它是否是这两个数字之间整数的一部分,而简单的范围检查(>=,更清晰的写入方法是:

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}')
def运行检查(num、low、high):

is_or_not='is'如果低,你希望它精确返回什么?0.5不在0和1之间
ran_check(6,4,12)
对这两个函数都有效,你确定你没有覆盖函数吗?笔记本电脑的全局作用域可能是有害的。如果两个函数都低,写这篇文章的更清晰方法是
,如果两个函数都“不返回”,顺便说一句,
low range()返回一个数字生成器,
step
aparted,而不是严格意义上的整数range()只能将整数作为参数,至少据我所知,因此我认为没有任何组合不能使其输出整数。是的,你是对的。但仍然无法返回正确的数组,我只是认为差异与问题无关。。。