-2

In Python, I have code where I only want the input to be a number and I want there to be an error message if the user inputs a string(words). How do I do that?

2 Answers2

1

use try and except

userInp = input("YOUR QUESTIONS")
try:
    float(userInp)
except ValueError:
    print("YOUR ERROR")
0

You can have the input inside a while loop. In the loop you can check the type of the input (make sure to assign it to a variable). Have a condition that exits the loop when the input is a number and give an error message when it is not

markb
  • 281
  • 2
  • 8