FAQ: Code Challenge: Loops - Greetings

This community-built FAQ covers the “Greetings” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Greetings

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I genuinely don’t know why this is wrong. May I get some help? I don’t want them to just give me the answer; I want my logic to be adjusted.

def add_greetings(names):
  for i in names:
    names.append('Hello, ' + i)

EDIT: So I figured out that I’m causing an infinite loop. but now my code looks like this:

def add_greetings(names):
  greeting = ["Hello, " + i for i in names]
  names.append(greeting)

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

But my output is “None”

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!

2 Likes

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?

You may want to try instead:

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.

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
2 Likes

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.

Hi @system5811963879,

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]
1 Like

What’s wrong with my code? All I’m getting is “None” which is not telling me too much :confused:

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]
1 Like

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