Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Breaking out of while loop is not working

I have a code sample where the problem is:

while True:
  for i in range(3):
    i = input()
    if i == 'quit'
      break
    else:
      print('Try again')

When I type quit in my input it doesn’t break out of the code. Thanks for your help!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try using this code:

while True:
  i = input()
  if i == 'quit'
    break
  print('Try again')

I removed for loop because it has no usage and for loop is the reason of break not working because you are breaking out of for loop and not the while loop. I also removed else in if statement.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading