如何正确使用其他函数/方法返回的变量?

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

请多多包涵,因为我刚开始学习编码,Python 是我的第一门语言。 我很挣扎,无法真正理解这些功能是如何工作的。 我无法调用它并在以后需要它时在另一个函数中使用它。

有人可以帮助我了解它的深度吗?

我的代码不起作用,我无法理解如何从函数中获取结果,以便将这些结果用于最终目的。

这是我在我正在从事的项目中尝试过的东西:

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') 时,我得到这个错误并且我知道它来自哪里,“name 'strengths_of_coding' is not defined”,但我只是不知道如何访问该变量... 我被困在这里,有点沮丧,因为我知道这是一个菜鸟错误,但在经历了一周的压力和挫折之后,我仍然无法掌握它。

欢迎任何反馈。

strengths_of_coding 仅在 analise_and_explain_results 函数内部定义。当您 return 该函数的值时,它们不再附加到您在函数内部使用的名称

return count_man_words, count_woman_words, strengths_of_coding 也可以写成 return (count_man_words, count_woman_words, strengths_of_coding) - 这意味着函数的 return 值是一个包含 3 个元素的元组,这些元素是每个变量的值,并且元组分配给 res in res = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)

函数内名为 strengths_of_coding 的变量的值在对 res

进行赋值后可在 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 中,当您 return 向一个变量添加多个内容时,该变量将变成一个元组。您可以提供多个变量来获取每个 return。例如,count_man_words, count_woman_words, strengths_of_coding = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)。或者,如果您只需要那个 return 那么 strengths_of_coding = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)[2].

因此,如果您几乎不了解 OOP 或一般编码,则很难解释所有内容。但在 python 中,函数的 return 值可以是任何值。 None,整数,列表,元组,字典,对象。甚至可以是 class 定义。只有看看它,你才会确切地知道。这就是所谓的鸭子打字; “如果它走路像鸭子,叫起来像鸭子,那么它一定是鸭子”

在这种情况下,您的 analise_and_explain_results 函数不会 return 一个 事情,而是几个因为它这样做:

return count_man_words, count_woman_words, strengths_of_coding

所以它实际上 return 是一个包含这三个值的元组。这些变量是 scoped 到那个特定的函数,你不能再在那个函数之外使用它们。 :为了简单起见;让我们坚持不要在函数之外使用它们,因为这是不好的做法。

然后在您的代码中执行此操作:

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("results are: %s" % results)

例如在最后添加它,您可以查看是否最终得到了正确的结果,就像在我的示例中一样,或者您可以将它添加到您的代码中以查看您在每个 return 中得到了什么结果功能等等。

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)