1

I'm working on an assignment where I get a user's input to check if their input is a float or not. I've got mostly all the cases down, but how could I go about the case where the user enters more than one decimal ('.') in their input? (i.e.: 431..541)?

Zyanaster
  • 29
  • 7

1 Answers1

1
def is_valid_float(string):
    try:
        float(string)
    except ValueError:
        return False
    else:
        return True
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Although your code shows how to turn an exception or not into a boolean response, which is useful to know, I suspect the assignment is to do the check without using `float`. – Terry Jan Reedy Feb 25 '15 at 19:19
  • it may be so but until OP says that `float()` is not allowed; `float()` is the preferable solution. [It is rather complicated otherwise](http://stackoverflow.com/a/385597/4279). – jfs Feb 25 '15 at 19:29