Python return function question.

Asked 2 years ago, Updated 2 years ago, 17 views

I looked it up, but I don't understand why it's not working

def get_amount_by_keyword_id(self,keword) :
    uri = '/ncc/keywords'
    method = 'GET'
    r = requests.get(BASE_URL + uri, params={'nccAdgroupId': self.get_adgroup_id()} ,headers=self.get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID))
    for i in r.json():
        if(i['keyword'] == keyword): # Search keyword
            amount = i['bidAmt']
    return amount

First, this is a function with a return

def Call(self):
      for q in range(0,Lastindex+1):
        print("amount:" + str(self.get_amount_by_keyword_id(lines[q]).strip())) + "circle")

lines[q] You have to call up the text in that way and use a low function for each text.

return amount UnboundLocalError: local variable 'amount' referenced before assignment I have this error, so I'm asking you a question Thank you for your answer. I tried my best to write it down. If you don't understand the question, please leave a comment I'm sorry. Thank you

python

2022-09-20 11:05

1 Answers

if (i['keyword'] == keword):
  amount = i['bidAmt']

It seems that there was no initialization of the amount variable because none of these conditional expressions were satisfied.

Please run the code below:

def fn(n):
  if (n == 1):
    amount = 'return value'
  return amount

fn(2) # UnboundLocalError: local variable 'amount' referenced before assignment


2022-09-20 11:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.