Is the following code conventionally acceptable and what are some problems that may arise from implementing the required solution in such a manner?
while True:
Iq = input("What is your Iq? ")
if Iq.isdigit():
print(Iq)
break
else:
print("that is not a number")
Potential problems with using
.isdigit()to validate numeric input are:Even if you do want to restrict the user to entering positive whole numbers, you might want to have different error messages for negative/non-integer inputs than for completely non-numeric inputs such as
abcor the empty string.Here's a more robust input function, which allows negative or non-integer inputs, and also supports optional bounds-checking for the value.