-2

This is a line I have in my source code:

r = float(input("\nEnter radius: "))

and my professor wants me to check if the input is a float AFTER I have given user input i.e. "four".

How can I do this?

nyawng
  • 5
  • 2
  • 1
    Catch the `ValueError` that `float` raises when the conversion fails – Brian61354270 Aug 17 '23 at 01:11
  • It is POSSIBLE (although tricky) to write a regular expression to check for floating values, but it is much easier to let Python do it and use `try` / `except ValueError`. – Tim Roberts Aug 17 '23 at 01:18
  • Accept their answer without caring whether it is a float, then call `float()` on the answer. What is the difficulty? – John Gordon Aug 17 '23 at 01:46

0 Answers0