0

how are you all doing today? I have this code below, requesting in an API

import requests 


def Sults():
  headers = {
        "xxxxxxxxxxxxx",
        "Content-Type":"application/json;charset=UTF-8"
    } 
  url = "https://xxxxxxxxx/api/v1/implantacao/projeto?&dtInicio=2022-08-06T18:02:55Z"
  response = requests.get(url, headers=headers)
  data = response.json() 
  
  print(data) 
  unidades(data)
  testFunction()


def unidades(data):
  id = []
  
  for i in data['data']:
    print(i['nome'])
    print(i['dtInicio'])
    print(i['dtFim'])
    id.append(i['id'])
    print(i['responsavel']['nome'])
    print(i['modelo']) 

  
         
def testFunction():
  

How can i use the id list into this def testFunction? This id list for example returns this into the actual code: [1,2,5,6,7,9,10]

  • You can either make id global or pass it as an argument of `testFunction` by returning the list from `unidades`. Also, I would suggest you not to name the list as id because id is a built in function in python – Nehal Birla Jan 31 '23 at 18:28
  • Please see [How do I get a result (output) from a function? How can I use the result later?](/questions/3052793). I would close the question as a duplicate, but I am out of close votes for today. – Karl Knechtel Jan 31 '23 at 18:29
  • Does this answer your question? [How do I get a result (output) from a function? How can I use the result later?](https://stackoverflow.com/questions/3052793/how-do-i-get-a-result-output-from-a-function-how-can-i-use-the-result-later) – Nehal Birla Jan 31 '23 at 18:30

3 Answers3

0

Return the list from unidades() and then pass it as a parameter to testFunction().

def Sults():
  headers = {
        "xxxxxxxxxxxxx",
        "Content-Type":"application/json;charset=UTF-8"
    } 
  url = "https://xxxxxxxxx/api/v1/implantacao/projeto?&dtInicio=2022-08-06T18:02:55Z"
  response = requests.get(url, headers=headers)
  data = response.json() 
  
  print(data) 
  ids = unidades(data)
  testFunction(ids)

def unidades(data):
  id = []
  
  for i in data['data']:
    print(i['nome'])
    print(i['dtInicio'])
    print(i['dtFim'])
    id.append(i['id'])
    print(i['responsavel']['nome'])
    print(i['modelo']) 

  return id

def testFunction(id):
  # use id
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Depends on the actual code you'll be writing. What I can currently think of is if you make unidades() return the id list, and then add that into testFunction() as a parameter :D

Like so:

def unidades(data):
   id = []

   for i in data['data']:
       print(i['nome'])
       print(i['dtInicio'])
       print(i['dtFim'])
       id.append(i['id'])
       print(i['responsavel']['nome'])
       print(i['modelo'])
   return id

def Sults():
    # Do all the other stuff
  
    id_list = unidades(data)
    testFunction(id_list)
sami0505
  • 42
  • 3
0

Bad idea... but you can do like this... better off declaring a class

declare it global

import requests 
id=[]

def Sults():
  global id
  headers = {
        "xxxxxxxxxxxxx",
        "Content-Type":"application/json;charset=UTF-8"
    } 
  url = "https://xxxxxxxxx/api/v1/implantacao/projeto?&dtInicio=2022-08-06T18:02:55Z"
  response = requests.get(url, headers=headers)
  data = response.json() 
  
  print(data) 
  unidades(data)
  testFunction()


def unidades(data):
  global id
  id = []
  
  for i in data['data']:
    print(i['nome'])
    print(i['dtInicio'])
    print(i['dtFim'])
    id.append(i['id'])
    print(i['responsavel']['nome'])
    print(i['modelo']) 

  
         
def testFunction():
   global id
geekay
  • 340
  • 1
  • 5