简体   繁体   English

对 Python 编程有点陌生,正在尝试一个问题。 我只是不知道为什么这个错误不断弹出。 对我来说真的没有意义

[英]Kinda new to Python Programming and was attempting a question. I just don't know why this error keeps popping up. Really doesn't make sense to me

This was the question: Create a function named add_greetings() which takes a list of strings named names as a parameter.这就是问题所在:创建一个名为add_greetings()的 function,它将名为名称的字符串列表作为参数。

In the function, create an empty list that will contain each greeting.在 function 中,创建一个包含每个问候语的空列表。 Add the string "Hello, " in front of each name in names and append the greeting to the list.在名称中的每个名称前添加字符串"Hello, " ,并在列表中添加问候语 append。

Return the new list containing the greetings.返回包含问候语的新列表。

Quite a basic question but I accidentally initialized the new list outside the function like this:相当基本的问题,但我不小心初始化了 function 之外的新列表,如下所示:

new_lst = []

def add_greetings(names):
    for name in names:
        name = "Hello, " + name
        new_lst.append(name)
    return new_lst

print(add_greetings(["Owen", "Max", "Sophie"]))

This was a question on Codecademy and it gave me this error:这是 Codecademy 上的一个问题,它给了我这个错误:

add_greetings(["Owen", "Max", "Sophie"]) should have returned ['Hello, Owen', 'Hello, Max', 'Hello, Sophie'] , and it returned ['Hello, Owen', 'Hello, Max', 'Hello, Sophie', 'Hello, Owen', 'Hello, Owen', 'Hello, Max', 'Hello, Sophie'] add_greetings(["Owen", "Max", "Sophie"])应该返回['Hello, Owen', 'Hello, Max', 'Hello, Sophie'] ,它返回['Hello, Owen', 'Hello, Max', 'Hello, Sophie', 'Hello, Owen', 'Hello, Owen', 'Hello, Max', 'Hello, Sophie']

Why is this the case?为什么会这样? Tried it on the Python IDE, and it gave me the expected correct answer.在 Python IDE 上尝试过,它给了我预期的正确答案。

You should try instantiating the new_list inside the function:您应该尝试在 function 中实例化 new_list:

def add_greetings(names):
    new_lst = []
    for name in names:
        name = "Hello, " + name
        new_lst.append(name)
    return new_lst

print(add_greetings(["Owen", "Max", "Sophie"]))

What ends up happening when Codeacademy tests it is they will run more than one test case.当 Codeacademy 测试时最终会发生什么,它们将运行多个测试用例。 When you run your method more than once, it will continue appending to the same list over and over again, and will therefore return the wrong result.当你多次运行你的方法时,它会一遍又一遍地追加到同一个列表中,因此会返回错误的结果。 This can be mitigated by putting the list initialization inside the function itself, so you start fresh each time.这可以通过将列表初始化放入function 本身来缓解,因此您每次都重新开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道为什么这个 Python 编程代码不起作用 - I don't know why this Python Programming code doesn't work Python SimpleHTTPServer一直在下降,我不知道为什么 - Python SimpleHTTPServer keeps going down and I don't know why 我使用 Python 制作了一个非常基本的脚本(我是新手),我不知道为什么它不工作,因为它应该工作 - I made a really basic script using Python (I am new at it) and i don't know why it is not working as it should work 在python中re.split的输出对我来说没有意义 - the output of re.split in python doesn't make sense to me 我得到这个错误,我真的不知道它是什么意思 - I get this error and I really don't know what it means 我的程序一直循环,我不知道为什么 - my program keeps looping and I don't know why 我不知道为什么 python 会抛出这个错误 - I don't know why python is throwing this error 我正在努力从叶子上盖起霍夫曼树。 我有我的排序列表,但不知道如何进行 - I am struggling to build a Huffman tree from the leaves up. I have my sorted list, but don't know how to proceed 我不知道为什么表单没有显示在我的页面上 - I don't know why form doesn't show up on my page 重复发生的 Python 错误没有多大意义 - Reoccurring Python error that doesn't make much sense
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM