You’re going to want to add a “return greeting” after names.append(greeting).
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"]))
Consider removing the (
in front of name
.
You don’t need the append.
This should do:
def add_greetings(names):
greeting = ["Hello, " + i for i in names]
return greeting
Hello,
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.
Managed to fit the whole list comprehension in the return statement to keep it clean.
def add_greetings(names):
return ["Hello, " + i for i in names]
What’s wrong with my code? All I’m getting is “None” which is not telling me too much
def add_greetings(names):
greetings =
full_greeting = ('Hello, ’ + name for name in names)
greetings.append(full_greeting)
Your code is not returning the final list, hence the default return value, None
.
Ok, now my code looks like this:
def add_greetings(names):
greetings = []
full_greeting = ('Hello, ' + name for name in names)
greetings.append(full_greeting)
return full_greeting
And this is what get:
<generator object add_greetings.. at 0x7f4cf16d7518>
What object are you growing from scratch? That’s the one to return.
Ok, I tried to return greetings, I got this:
[<generator object add_greetings.. at 0x7fc11d733518>]
That’s because your are returning a generator.
('Hello, ' + name for name in names)
If you wanted, you could use a list comprehension so a list is returned. It would not need to initialized, or even bound to a variable…
return ['' + _ for _ in object]
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
Any idea what is wrong with the code?
Regards
Mani
I would start by unraveling that line.
* `hello` is defined as a str
* list.append() returns undefined
I’ve done this and it is working
def add_greetings(names):
list =
for name in names:
string = 'Hello, ’ + name
list.append(string)
return list
It’s all about indentation. I was trying to figure out why my code didn’t worked.
def add_greetings(names):
lst=[]
for i in names:
lst.append("Hello, "+i)
return lst
Then I notice that other similar codes with list comprehension.
def greet(names):
message = [f"Hello, {name}" for name in names]
return message
Hello everyone, My Name is Lazar. I am new in Python.
I done it this way. Took a bit struggle.