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!

1 Like

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

my solve for this:

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