I am doing some calculations on a dictionary. But the important thing is I want to make a if-condition that kind of says
"if x has a value that is closer to 1 (or equal to 1) than variable "
kind of hard to explain, but hope you understand.
You can use absolute value :
if abs(x-1) < abs(variable-1):
...
since absolute value of x-1
is the distance between x
and 1
, and similarly absolute value of variable-1
is the distance between variable
and 1
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.