The float()
function used on any other string other than a decimal number, 'nan'
, '-inf'
or 'inf'
(or slight variants of those strings) will throw a ValueError
instead, because anything else is not a valid floating point value. math.isnan()
only works on the special float value float('nan')
, but it is not ever called if float()
raises an exception first.
Use exception handling to catch that error instead:
try:
diceChoice = int(diceChoice))
except ValueError:
print(diceChoice, 'is an invalid choice')
I used int()
instead because your input requires that the user enters a whole number.
Just to be explicit: float('nan')
is a specific floating point value to signify the output of certain mathematical operations that are not an exception. It is never used to signify invalid input.