簡體   English   中英

如何正確使用其他 function / 方法中返回的變量?

[英]How can I properly use variables returned in other function / method?

由於我剛開始學習編碼,請與我裸露,Python 是我對 go 的第一語言。 我很掙扎,無法真正理解這些功能是如何工作的。 當我在另一個 function 中需要它時,我無法調用它並稍后使用它。

有人可以幫我理解它的深度嗎?

我的代碼不起作用,我無法理解如何從 function 中獲取結果,以便將這些結果用於最終目的。

這是我在我正在從事的項目中嘗試過的:

manly_coded_bag = []
female_coded_bag = []

feminine_coded_words = [
    "agree",
    "affectionate",
    "child",
    "cheer",
]

masculine_coded_words = [
    "active",
    "adventurous",
    "aggressive",
    "ambitios",
]

explanations = {
    "feminine-coded": (
        "This job ad uses more words that are subtly coded as feminine than words that are subtly coded as masculine"
    ),
    "masculine-coded": (
        "This job ad uses more words that are subtly coded as masculine than words that are subtly coded as feminine."
    )


def men_coded_words(masc_bag, text):
    add_text = text
    man_coded_bag = masc_bag
    for word in masculine_coded_words:
        if word in add_text:
            man_coded_bag.append(word)    
    return man_coded_bag


def women_coded_words(fem_bag, text):
    add_text = text
    woman_coded_bag = fem_bag
    for word in feminine_coded_words:
        if word in add_text:
            woman_coded_bag.append(word)
    return woman_coded_bag


def analise_and_explain_results(text, count_man, count_fem):
 
    count_man_words = count_man
    count_man_words = len(man_coded_bag)

    count_woman_words = count_fem
    count_woman_words = len(woman_coded_bag)

    coding_score = count_woman_words - count_man_words

    strengths_of_coding = ""

    if coding_score == 0:
        if count_man_words:
            strengths_of_coding = "neutral"
        else:
            strengths_of_coding = "empty"
    elif coding_score > 0:
        strengths_of_coding = "feminine-coded"
    else:
        strengths_of_coding = "masculine-coded"

    return count_man_words, count_woman_words, strengths_of_coding


def get_results(text):
    user_input = text
    user_input = input("add text here:").lower()

    res = analise_and_explain_results(text, man_coded_bag, 
    woman_coded_bag)


    # i am trying to use the returned variable strengths_of_coding and 
    is not accesible.
    explain_results = explanations[strengths_of_coding]

    return res, explain_results

get_results("random text added here, really whatever for testing purposes")

是的,所以當我調用 get_results('text') 時,我收到了這個錯誤,我知道它來自哪里,“名稱 'strengths_of_coding' 沒有定義”,但我只是不知道如何訪問該變量。 .. 我被困在這里並且有點沮喪,因為我知道這是一個菜鳥錯誤,但經過一周的壓力和沮喪之后,我仍然無法掌握它。

歡迎任何反饋。

strengths_of_coding僅在analise_and_explain_results function 中定義。 當您返回 function 的值時,它們不再附加到您在 function 中使用的名稱

return count_man_words, count_woman_words, strengths_of_coding也可以寫成return (count_man_words, count_woman_words, strengths_of_coding) - 這意味着 function 的返回值是一個元組,其中包含 3 個元素,這些元素是每個變量的值,並且該元組被分配給resres = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)

分配給res后,function 中名為strengths_of_coding的變量的值在get_results中可用作res[2]

res = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)res變成具有 3 個元素的元組。 strengths_of_coding是這個元組中的第三個元素。 因此,您以res[2]的形式訪問它。 在 python 中,當您將多個內容返回給一個變量時,該變量會變成一個元組。 您可以提供多個變量來獲取每個回報。 例如, count_man_words, count_woman_words, strengths_of_coding = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag) 或者,如果您只需要返回一個,則strengths_of_coding = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)[2]

因此,如果您對 OOP 或一般編碼幾乎一無所知,就很難解釋一切。 但是在 python 中,function 的返回值可以是任何值。 None ,integer,列表,元組,字典,object。 甚至可以是一個 class 定義。 只有看它,你才能確切地知道。 這就是所謂的鴨式打字; “如果它走路像鴨子,叫起來像鴨子,那它一定是鴨子”

在這種情況下,您的analise_and_explain_results function 不會返回件事,而是返回幾件事,因為它這樣做了:

return count_man_words, count_woman_words, strengths_of_coding

所以它實際上返回了一個包含這三個值的元組。 並且這些變量的范圍僅限於特定的 function,您不能再在 function 之外使用它們。 注意:為簡單起見; 讓我們堅持不要在 function 之外使用它們,因為這是不好的做法。

然后在您的代碼中執行以下操作:

res = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)

這意味着此時的res實際上是包含您感興趣的三個值的元組。您有幾種方法可以解決這個問題。 但最容易遵循的方法是像這樣分配變量的值:

count_man_words, count_woman_words, strengths_of_coding = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)

這基本上將元組解包為三個不同的值,因為它基本上是這樣做的:

a, b, c = (1, 2 ,3)

你之前在哪里:

d = (1, 2, 3)

拆包很容易,只要您拆包的物品包含您嘗試分配的物品數量即可;

a, b, c = d

如果您在掌握 OOP 和 python 時遇到困難,我建議您在跑步之前先學會走路,這是您現在正在做的 IMO。 遵循一些解釋 OOP 和 python 的教程或視頻。 或者像在realpython上那樣組合它們。

對不起,遲到的答案。 這就是我最終在回答的人的幫助下修復我的代碼的方式,我開始明白我在哪里犯了錯誤。 這就是我使用上面的示例修復代碼的方式,以防其他人難以掌握基礎知識。

作為任何新的初學者發現這很有用的獎勵,這是我從別人那里學到的東西。 最后一個 print 語句是調試代碼的一種非常有用的方法:

print("results are: %s" % results)

例如,在最后添加它,您可以查看是否最終獲得了正確的結果,就像在我的示例中一樣,或者您可以將其添加到您的代碼中以查看您在每個函數中返回的結果等等。

user_input = "Active, child, whatever random text, testing"
text = user_input.lower()

# declare the coded words, feminine and masculine
feminine_coded_words = [
    "agree",
    "affectionate",
    "child",
    "cheer",
]

masculine_coded_words = [
    "active",
    "adventurous",
    "aggressive",
    "ambitios",
]

# declare explanations to use when we explain the results to the user.
explanations = {
    "feminine-coded": (
        "This job ad uses more words that are subtly coded as feminine than words that are subtly coded as masculine"
    ),
    "masculine-coded": (
        "This job ad uses more words that are subtly coded as masculine than words that are subtly coded as feminine."
    ),
    "neutral": ("this is neutral"),
    "empty": ("empty text"),
}


# initiate two empty variable where we add our coded words, when we find them.


def men_coded_words(text):
    add_text = text
    manly_coded_bag = []
    for word in masculine_coded_words:
        if word in add_text:
            manly_coded_bag.append(word)
    return manly_coded_bag


def women_coded_words(text):
    add_text = text
    feminine_coded_bag = []
    for word in feminine_coded_words:
        if word in add_text:
            feminine_coded_bag.append(word)
    return feminine_coded_bag


def feminine_counted_words(text):
    woman_coded_bag = women_coded_words(text)
    return len(woman_coded_bag)


def masculine_counted_words(text):
    man_coded_bag = men_coded_words(text)
    return len(man_coded_bag)


def coding_score(text):
    count_fem_words = feminine_counted_words(text)
    count_masc_words = masculine_counted_words(text)
    return count_fem_words - count_masc_words


def explain_results(text):
    strengths_of_coding = ""
    count_masc_words = masculine_counted_words(text)
    coding_score_results = coding_score(text)

    if coding_score_results == 0:
        if count_masc_words:
            strengths_of_coding = "neutral"
        else:
            strengths_of_coding = "empty"
    elif coding_score_results > 0:
        strengths_of_coding = "feminine-coded"
    else:
        strengths_of_coding = "masculine-coded"

    return strengths_of_coding


def get_results(text):
    strenght_of_coding = explain_results(text)

    return explanations[strenght_of_coding]


results = get_results(text)

print("results are: %s" % results)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM