How to break out of function and back into loop?
<p>I am currently making a web builder where it will generate a random gif based on the GIPHY API. I am having a problem where I test a case on when the api returns with 0 results.</p>

<pre><code>def get_image_link(link):
global flag
set_count = 0
r = requests.get(link)
api_response = json.loads(r.text)
response = api_response['data']
if not response:
    print('GIPHY API returned no results... finding another word...')
    pass
elif response:
    for set in response:
        set_count += 1
    random_gif_num = random.randint(0, set_count) - 1
    try:
        flag = True
        return response[random_gif_num]['images']['original']['url']
    except TypeError:
        print(TypeError + '... rerunning application...')
        pass
</code></pre>

<p><code>while not flag:
    get_image_link(get_random_query())</code></p>

<p>Essentially, if the results come back with no data in the results, i want it to retry the function to grab another word. The program works when a word with results comes back but when it comes back with 0 results I get a <code>TypeError</code> and it doesn't go back into the loop. I am sure it does this because it doesn't break out of the function and instead returns a <code>[]</code> type. How can I break out of the function and get back into the while loop so I can generate another result? Thank you.</p>

<p>Reset flag to false! </p>

<pre><code>try:
    flag = True
    return response[random_gif_num]['images']['original']['url']
except TypeError:
    flag = false
    print(TypeError + '... rerunning application...')
    pass
</code></pre>

python