首页 > 解决方案 > 任何人请向我解释这个问题,我知道我到底要编码什么?

问题描述

几乎在那里:给定一个整数 n,如果 n 在 100 或 200 的 10 以内,则返回 True

几乎_there(90) --> 真

几乎_there(104) --> 真

几乎那里(150)-> 假

几乎_there(209) --> 真

注意: abs(num) 返回数字的绝对值

标签: pythonfunction

解决方案


您可以使用if n in range(x, y):. 例如,在下面的代码中应用了 90 到 100 的范围。如果它在此范围内,则设置almost_there为 True。否则将其设置为 False。

def almostThere(n):
    if n in range(90,100):
        almost_there = True
    else :
        almost_there = False

您的问题的完整解决方案将是:

def almostThere(n):
    if n in range(90,110) or n in range (100,110) or n in range (190,210):
        almost_there = True
    else :
        almost_there = False

推荐阅读