Why doesn’t this work? I mean technically it does, but without providing a return statement I have to deal with “None” showing up at the right
When I try to write the code with “return” instead of “print” I get only “Hello, Owen”. Why doesn’t it loop through all of the names?
This is literally just me trying to figure out alternate paths to the same solution–I’ve already completed the assignment the way it asked. Thank you for your help!
I can’t fix your logic because I’m at the same level as you, but I was told in a previous post I made that “none” shows up because the program CodeAcademy uses expects some kind of return statement within the functions you create.
This was for a different project, but I also had “None” show up in this project and adding return worked. Maybe try “return names.append(greeting)”
Can anyone help to explain why the code checker seems to think my list is twice as long as it is?
I saw the solution which defined an empty list in a line before my for loop instead of in the arguments for the function. (only different) what are the implications declaring an empty list in the function vs as an argument?
def add_greetings(names):
lst=
for i in names:
lst.append("Hello, " + i )
return lst
It’s better to put the list inside the function like this because every time it’s called it empties the list.
If you use a list as an argument it will only be empty the first time you use it. If the function get called again later in the script the list will still have the last set of values that were assigned to it on the first function call.
Try calling your version of the script twice like this:
#Uncomment the line below when your function is done
print(add_greetings([“Owen”, “Max”, “Sophie”]))
print(add_greetings([“Owen”, “Max”, “Sophie”]))
You’ll see that it will give you an output that is similar to the code checker.
Hi all. I’m getting the following error for my code below:
File “script.py”, line 7
return greeting
^
SyntaxError: invalid syntax
I’m not really sure why? my understanding is its suggesting that i’ve made a mistake when trying to return greeting but i can’t figure out what i’ve done wrong?
#Write your function here
def add_greetings(names):
greeting=[]
for name in names:
greeting.append("Hello, " + (name)
return greeting
#Uncomment the line below when your function is done
print(add_greetings(["Owen", "Max", "Sophie"]))
I’m trying to figure out why placing my new_list outside of the function defined produces the text multiple times whereas placing the new_list (the new variable) inside the function does not. Example below.
def add_greetings(names):
new_list=
for name in names:
new_list.append("Hello, " + name)
return new_list
#Uncomment the line below when your function is done
print(add_greetings([“Owen”, “Max”, “Sophie”]))
This is the correct code however creating the new_list outside the function produces the following: 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’]
I’ve completed the exercise I’m just trying to understand why I get the error otherwise.
Your posted code is not formatted, therefore it does not exhibit important details, such as indentation. This makes it difficult to read and comprehend. For advice on how to format code for posting, see How to ask good questions (and get good answers)
Codecademy uses a submission correctness test (SCT) to check your code. With the current exercise, the SCT calls your add_greetings function with its own list of names in addition to executing your call to the function. Consequently, the functiion gets called more than once.
To gain some insight into the problem that you had originally, place the initialization of new_list outside the function, prior to its header, and call the function multiple times, with a different list each time. Let us know what pattern you notice, and why you think it occurs.
Edited on September 19, 2019 to add the following:
After you are convinced that you understand why the initialization needs to occur inside the function, you can restore it to its rightful position.
I am completely new in this programming field. I write the following code against this exercise
#Write your function here
def add_greetings(names):
** new_list = **
** hello = "Hello, "**
** for name in names:**
** combine = str(hello) + new_list.append(name)**
** return combine**
#Uncomment the line below when your function is done
print(add_greetings([“Owen”, “Max”, “Sophie”]))
But when I am executing the code, I am getting the following error message:
Traceback (most recent call last):
File “script.py”, line 12, in
print(add_greetings([“Owen”, “Max”, “Sophie”]))
File “script.py”, line 7, in add_greetings
combine = str(hello) + new_list.append(name)
TypeError: must be str, not NoneType