Introduction
The for loop
The while loop
Nested loops
break and continue
16. break with while loops
Summary

Instruction

Great! So far, our while loops only ended when the loop condition was satisfied. However, we can also decide to end a loop inside it:

user_input = input('Please provide a number: ')
while True:
  if user_input.isnumeric():
    break
  user_input = input('Not a number! Please provide a number: ')
print('Your number is:', user_input)

while True is used to introduce an infinite loop – the condition True is always true. However, inside the loop, we provided the following if statement:

if user_input.isnumeric(): 
  break

The break keyword is used to terminate the whole loop. Once the user provides a real number, the loop immediately finishes.

Exercise

Keep asking the user to write the year in which Python was first released: 'In which year was Python first released? '

When the answer is finally correct (1990), print: Correct!

Use a break statement.

Stuck? Here's a hint!

Remember to use int() to convert from string to int.