python - 在python中检查差异数小于10或大于10
问题描述
如何查找一个数字是否小于或大于 10 的差异。
示例 1:如果 a 是 100,b 是 91。这几乎是匹配。
示例 2:如果 a 是 100,b 是 89。这根本不是匹配。
下面是代码及其工作正常。有没有其他最简单或最好的方法来实现
a = 110
b = 100
c = a - b
d = a - 10
if a > b:
if (a - b) <= 10:
print "This is almost Matching"
else:
print "This is not at Matching"
else:
if (b - a) <= 10:
print "This is almost Matching"
else:
print "This is not at Matching"
预期和实际变得相同
解决方案
您需要寻找差异的绝对值(a,b)
:
该方法abs()
返回 x 的绝对值 - x 和零之间的(正)距离。
a = 100
b = 110
print(abs(a - b)) # 10
if abs(a -b) <= 10:
print("This is almost Matching")
else:
print("This is not at Matching")
输出:
10
This is almost Matching
推荐阅读
- javascript - 使用 javascript 将图像转换为字节数组
- java-7 - 尝试通过从一个方法读取相同的文本文件到java中的不同方法来优化代码并提高性能
- docker - 具有内部存储库的 GC Cloud Build 自定义构建过程
- python - HTTP 状态码的多线程脚本
- powershell - 如何判断脚本返回的退出状态码是1还是0
- python - 安装了 pytest 但在 bash 中运行 `pytest` 返回 `not found`
- c++ - 考虑到危险,为什么项目使用 -I include 开关?
- xslt - XSLT 1.0 循环遍历同一父节点的不同子节点
- amazon-dynamodb-dax - DynamoDB DAX 和高可用性
- c++ - /clang:-1: Xcode 上的链接器命令失败,退出代码为 1(使用 -v 查看调用)