Home > Software design >  Increasing a value each time a function passes through a for loop
Increasing a value each time a function passes through a for loop

Time:12-15

I am trying to make a for loop that returns response.json[i] from an API, with i increasing by 1 each time through the loop.

Example:

import requests
import json 

def get_url():
    url = "https://bing-news-search1.p.rapidapi.com/news/search"    

    querystring = {"q":"bitcoin","count":"20","textFormat":"Raw","safeSearch":"Off"}        

    headers = {
        'x-bingapis-sdk': "true",
        'x-rapidapi-host': "bing-news-search1.p.rapidapi.com",
        'x-rapidapi-key': "api_key"
        }       

    response = requests.request("GET", url, headers=headers, params=querystring)    
    print(response.json()["value"][i]["url"]) # Here is where I need i to increase each time
for i in range(10):
    get_url()

My goal is for get_url() to run 10 times, each time printing the response associated with the increasing i value. Sorry if this is worded poorly, I am a total beginner and 100% self taught. Thanks in advance!

CodePudding user response:

Add i as an argument to get_url:

def get_url(i):

and then add it to your get_url call in your for loop:

for i in range(10):
    get_url(i)
  • Related