while loops we've seen so far were pretty predictable in terms of the number of iterations. However, there are also situations when we don't know the exact number of iterations. Take a look:
user_input = input('Please provide a number: ')
while not user_input.isnumeric():
user_input = input('Not a number! Please provide a number: ')
print('Your number is:', user_input)
We can't say for sure how long our user will take to provide a number instead of letters or special characters. We simply want our loop to repeat as long as the input is not numeric. Once the user provides a number, the loop immediately finishes.
Note how we used
isnumeric() is a standard Python function that can be used to check whether any string denotes a numerical value.