Introduction
The for loop
The while loop
11. while loops for user input
Nested loops
break and continue
Summary

Instruction

Good. The 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 user_input.isnumeric(). isnumeric() is a standard Python function that can be used to check whether any string denotes a numerical value.

Exercise

You can see that there is a secret_number in the template code and the question to the user to guess it. Your task is to write a loop that will keep asking the user for the generated number until their guess equals the secret number.

If the number is wrong, print: Wrong! Try again (0-9):

If the number is right, print: Correct!

Stuck? Here's a hint!

Use the following termination condition:

while user_input != secret_number:

Remember to convert the user input, which is a string, into an integer using int().