python - 任何人请向我解释这个问题,我知道我到底要编码什么?
问题描述
几乎在那里:给定一个整数 n,如果 n 在 100 或 200 的 10 以内,则返回 True
几乎_there(90) --> 真
几乎_there(104) --> 真
几乎那里(150)-> 假
几乎_there(209) --> 真
注意: abs(num) 返回数字的绝对值
解决方案
您可以使用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
推荐阅读
- python - 将 typedef 结构从 C++ 转换为 Python
- c - 我找不到分段错误:11 错误
- grpc-node - Node.js node_modules\grpc\src\node\extension_binary\node-v83-win32-x64-unknown\grpc_node.node
- c# - Linux Headless Server (.NET) 上的 Google OAuth2 授权
- c# - C# Entity Framework 连接表
- flutter - 在颤动中更改上标签栏时如何有条件地显示底部导航栏
- jax-rs - 如何从 MultipartFormDataInput 获取 InputStream?
- ios - 应用程序运行时更新领域 IgnoredProperties
- java - 如何改进与多个客户的 Java 聊天?
- r - 如何从数据框中的一个位置复制一个值并将其应用于列的其余部分(循环?应用?)?