下面的代码演示如何编写一个名为“greater_than”的函数,以确定区间[start,end]内的所有数字是否大于给定数字“threshold”。如果大于该值,该数字将被添加到列表中,并且最后返回该列表。
def greater_than(start, end, threshold):
result_list = []
for i in range(start, end+1):
if i > threshold:
result_list.append(i)
return result_list
要调用此函数并获取结果,只需提供开始和结束数字以及您想要比较的阈值:
start = 1
end = 20
threshold = 10
result = greater_than(start, end, threshold)
print(result)
在上面的示例中,我们调用了greater_than(1,20,10),它会找到区间[1,20]中所有大于10的数字,并将它们添加到一个列表中。代码将返回结果列表,并以下列格式打印结果:
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]