I have a question regarding the function in Python.

Asked 2 years ago, Updated 2 years ago, 48 views

def income_sheet_Y(code):
    # Importing data

    fs_rpt_url = "http://comp.fnguide.com/SVO2/ASP/SVD_Finance.asp?pGB=1&gicode=A{}&cID=&MenuYn=Y&ReportGB=&NewMenuID=103&stkGb=701".format(code)
    fs_rpt_res = requests.get(fs_rpt_url)
    soup = BeautifulSoup(fs_rpt_res.text, "lxml")

    # Creating a Data Frame

    data_column = ['0', '1', '2', '3', '4', '5']
    income_sheet_Y_df = pd.DataFrame(data=None, columns=data_column, index=['0'])


    # Find data
    data_rows = soup.find("div", attrs = {"id":"divSonikY"}).find("tbody").find_all("tr")

    for row in data_rows:
        columns = row.find_all("td")
        data_row = [column.get_text().strip() for column in columns]
        income_sheet_Y_df = income_sheet_Y_df.append(pd.Series(data_row, index=income_sheet_Y_df.columns), ignore_index=True)

    # Treat data as a number

    income_sheet_Y_df['0'] = income_sheet_Y_df['0'].str.replace(',', '')
    income_sheet_Y_df['1'] = income_sheet_Y_df['1'].str.replace(',', '')
    income_sheet_Y_df['2'] = income_sheet_Y_df['2'].str.replace(',', '')
    income_sheet_Y_df['3'] = income_sheet_Y_df['3'].str.replace(',', '')
    income_sheet_Y_df['4'] = income_sheet_Y_df['4'].str.replace(',', '')
    income_sheet_Y_df['5'] = income_sheet_Y_df['5'].str.replace(',', '')
    income_sheet_Y_df = income_sheet_Y_df.replace('', np.NaN)
    income_sheet_Y_df = income_sheet_Y_df.apply(pd.to_numeric, errors = 'coerce')
    income_sheet_Y_df = income_sheet_Y_df.fillna(0)

    # Make your data pretty
    columns = soup.find("div", attrs = {"id":"divSonikY"}).find("thead").find_all("th")
    data_column = [column.get_text().strip() for column in columns][1:7]

    financial_index = ['Exercise', 'Sales', 'Sales Cost', 'Sales Gross Profit', 'Sales and Management Expense', 'R&D Expense', 'Advertising Expense', 'Sales Expense', 'Other Cost Cost', 'Operating Profit', 'Financial Profit', 'Exchange-Value', 'Financial Loss or Loss or Loss', 'Financial Revenues'   Foreign exchange loss', 'death loss expense', 'fair value through profit or loss', 'loss on disposal of accounts receivable', 'loss on disposal of financial assets', 'loss on valuation of financial assets', 'loss on derivatives', 'other costs', 'interest income', 'dividend income', 'exchange income', 'disposal of inventory'', 'loss of assets', 'fair profit', 'profit', 'loss of assets', 'loss of assets','   Interest expense', 'foreign exchange loss', 'inventory loss', 'disposal loss', 'fair value-measured financial asset valuation loss through profit or loss', 'disposal loss', 'asset valuation loss', 'derivative loss', 'other loan loss', 'provision transfer', 'other', 'subsidiary, joint-contributing expense', 'subsidiary', 'subsidiary' and 'contributive profit or loss'
    income_sheet_Y_df.index = financial_index
    income_sheet_Y_df.columns = data_column
    come_sheet_Y = come_sheet_Y_df.drop ("practice")
    return income_sheet_Y
def income_sheet_Y(code):
        ........................
    return ......................

def valuation(code):
    income_sheet_Y = income_sheet_Y(code)

Hello, I'm a beginner at Python.

The question I'm going to ask you this time is about defining a function.

Like the second coding above, we're going to define a function called A, and we're going to run a function called A in the function called B

UnboundLocalError: local variable 'income_sheet_Y' referenced before assignment

I see this error.

I found out through the search that there are problems related to global variables and regional variables, but I have a few questions.

I'd like to upload all the codes, but it's too long to see, so I summarized it and posted a question.

I always get a lot of help from studying Python through hash code.

Thank you to everyone who answers.

python function

2022-09-20 17:54

2 Answers

I think there will be an error in the verification (code) section below.

def income_sheet_Y(code):
        ........................
    return ......................

def valuation(code):
    income_sheet_Y = income_sheet_Y(code)

The value of a variable is determined and is a fixed value used by other functions, which is known as an error that occurs when you try to change the value of that variable in a particular function.

In this case, declaring a variable with the global function can solve the problem, but... Too many variables with the same name will occur.

If you need to import the value of a particular variable several times, I understand that it works fine if you change the variable name that receives the value as follows.

def valuation(code):
    a = income_sheet_Y(code)
    b = income_sheet_Y(code)
    c = income_sheet_Y(code)
    d = income_sheet_Y(code)


2022-09-20 17:54

No need for a long explanation, just number two.

In places like Asp, you don't have to make a variable equal to the function name to return.


2022-09-20 17:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.