1

I have two methods inside a class. I want to call a method's variable inside another one. Here is my code:

#First Method:

def starter_loan_check(self):
    #global total_offer,final_offer
    #final_offer=[]
    for i in self.overall_data:
        if i[1]=='PS' and i[3] in conf.PS_State and i[5]>29:
            if i[2]!='challengerTUFT':
                for c in cutoff_config:
                    if i[3]==c['State'] and i[2]==c['challenger'] and i[4]<['CUTOFF3'] and i[4]>=['CUTOFF4']:
                        if i[7]>50:
                            global total_offer,final_offer
                            final_offer=[]
                            total_offer=[i for i in product(*[conf.i[3]['Loan_Amount'],conf.i[3]['Apr'],conf.i[3]['Term']])
                            #global total_offer,final_offer
                            #final_offer=[]
                            for t in total_offer:
                                if t[0]>min(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
                                    final_offer.append(t)
                                elif t[0]>max(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
                                    t[0]=max(i[3]['Loan_Amount'])
                                    final_offer.append(t)
                                elif i[9]>75:
                                    if t[0]>min(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
                                        final_offer.append(t)
                                    elif t[0]>max(i[3]['Loan_Amount']) and t[1] in i[3]['Apr'] and t[2] in i[3]['Term']:
                                        t[0]=max(i[3]['Loan_Amount'])
                                        final_offer.append(t)

And I am trying to access final_offer here: Second Method:

def starter_loan_logic(self):
    for i in  self.query1_data:
        for j in final_offer:
            if j[0]/(i[3]*.85)>.30:#LTI Check
                final_offer.remove(j)

            if i[2]==j[0] and i[4]=='BI_WEEKLY':
                PTI=i[5]/((i[3]*0.85)/12)>0.20
                final_offer.remove(j)

            if i[2]==j[0] and i[4]=='TWICE_PER_MONTH':
                PTI=i[6]/((i[3]*0.85)/12)>0.20
                final_offer.remove(j)

            if i[2]==j[0] and i[4]=='MONTHLY':
                PTI=i[7]/((i[3]*0.85)/12)>0.20
                final_offer.remove(j)

            for k in self.overall_data:
                k['state']=='IL' and i[5]/((i[3]*0.85)/12)>0.225
                final_offer.remove(j)

But I am getting an invalid syntax error at for t in total_offer Here I have tried Global but I am not sure whether I am using it correctly or not. please help me to understand that how can I access one function's variable inside another function.

4
  • 3
    Piece of advice: you should edit your post to reduce indentation (and also in your real editor). Also syntax error is often a typo/mixed tab/spaces. Please provide traceback Commented Apr 21, 2019 at 11:28
  • 2
    Whoo! Indentation alert! Indentation alert! Commented Apr 21, 2019 at 11:30
  • Indentation is supposed to be same overall right? Why is there double indentation after function definition? Does it compile in python , it shouldn't ? Commented Apr 21, 2019 at 11:32
  • Perhaps more important than fixing the indentation is proving a minimal working example that will elicit far better responses: stackoverflow.com/help/mcve Commented Apr 21, 2019 at 11:35

1 Answer 1

1

While it is possible to get what you are trying to work with global variables that would not be the recommended approach.

What you want to do is pass these variables into the function as parameters set by the caller. So the first method should look like:

def starter_loan_check(self, total_offer):
    final_offer = []
    << do loan check loop as above  - but don't initialize final_offer in the loop>>
    return final_offer

and the second loop would look like:

def starter_loan_logic(self, final_offer):
   <<do you loop here>>

Wherever you call the two functions you would need to pass the variables through for example:

total_offer = <<something>>
final_offer = self.starter_loan_check(total_offer)
self.starter_loan_logic(final_offer)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.