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

Continue the loop after an exception used in python

I am trying to run a loop with try and exception in python. Initially it runs for p=0 and j=1,2 then falls into the exception. the problem is that then I would like the loop to continue for p=1 and j=1,2,2….can you help me thanks! I tried something like this but it’s not working

for p in range(1):
    for j in range(23):
     try:

      b = response_json[0]['countries'][p]['names'][j]['values']
     except:
         break

>Solution :

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

Overall, using exceptions to handle normal flow is a bad pattern. Exceptions are for, well, exceptions.

Looks like you are trying to iterate over an array with missing keys. Instead of trying each key, you could iterate over the collection directly

country_data = response_json[0]['countries']

for country in country_data:
   for name in country['names']:
      b = name['values']
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