how to break out of while loop python code example

Example 1: infinite while python

#infinite While on Python

while True:
  print('Hello World!')

Example 2: python exit loop iteration

alphabet = ['a' , 'b' , 'c' , 'd' ]
for letter in alphabet:
  if letter == 'b' :
    continue
    #continues to next iteration
  print( letter )
for letter in alphabet:
  if letter == 'b' :
    break
    #terminates current loop
  print( letter )
for letter in alphabet:
  if letter == 'b' :
    pass
    #does nothing
  print( letter )

Example 3: how to use try except in while loop in python

while True:  try:    num = int(input("Enter an int: "))  except Exception as e:    print(e)  else:    print("Thank you for the integer!")    break# Enter an int: a# invalid literal for int() with base 10: 'a'# Enter an int: 3# Thank you for the integer