Comparing floating points numbers for equality is -almost- always a bad idea and a source of funny little bugs...
There are many examples like the classical:
>>> a = 1.0 / 3
>>> a
0.3333333333333333
>>> 1 - a == 2*a # 1 - a = 1 - 1/3 = 2/3 = 2*a
False
This kind of error is still present if we use the decimal module (more oriented to "accounting maths") but is solved if we use the fractions module:
>>> from fractions import Fraction
>>> c = Fraction(1, 3)
>>> 1 - c == 2*c
True
The best option is to define a range where the response is "good enough". there are several ways to do it, depending of what you want.
If the condition is for typical mathematical problem the best is to define a distance:
is_ok = abs(user_guess - secret_num) <= accuracy
This is essentially the same as
math.isclose(user_guess, secret_num, rel_tol=accuracy)
In this case if the target solution is 3.3312 with for example an accuracy of 0.005 that will mean that all the values between
3.3312 - 0.005 = 3.3262
and
3.3312 + 0.005 = 3.3362
will be considered as valid.
If we want a more "human like" check where any 3.33xxx... is valid but 3.329999 or 3.34 must be discarded the criteria must be:
def round_down(n, digits):
f = 10**digits
return int(n * f) / f
is_ok = 0.0 <= user_guess - round_down(secret_num, 2) < 0.01
In this case where it is also necessary to say if the guess is too small or too big something like:
# For "Human like"
lower = round_down(secret_number, 2)
upper = lower + 0.01
# For math like
# lower = secret_number - 0.005
# upper = secret_number + 0.005
if user_guess < lower:
print("Too low")
elif user_guess < upper:
print(f"You are close enough to the answer {user_guess} ~= {secret_number}")
else:
print("Too high")