0

I'm stuck with functions... I want to make a function that greets all the names I have in a list in 3 different ways: Good morning, Good afternoon and Good night.

I was able to reach the code below, but I'm not conffident that is the best way and in fact it isn't because the names in my list don't appear in the output.

My code is:

def greet(greeting, names):
    #return a greeting and then the list of names
    return (greeting)
    for name in names:
        return (f'- {name}.')

names = ['Phoebe', 'Rachel', 'Chandler']

#Say good morning to all
morning = greet('\nGood morning,', names)

#Say good afternoon to all
afternoon = greet('\nGood afternoon,', names)

#Say good night to all
night = greet('\nGood night,', names)

print (morning)
print (afternoon)
print (night)

Output:

Good morning,

Good afternoon,

Good night,

The expected output was suppose to be:

Good morning,
- Phoebe.
- Rachel.
- Chandler.

Good afternoon,
- Phoebe.
- Rachel.
- Chandler.

Good night,
- Phoebe.
- Rachel.
- Chandler.

What am I doing wrong?

1
  • return can only be used once inside a function. Try concatenating the string before returning. Commented Feb 20, 2022 at 13:44

3 Answers 3

2

return can only be used executed once inside a function. Try concatenating the string before returning.

def greet(greeting, names):
    #return a greeting and then the list of students
    str = greeting
    for name in names:
        str+= (f'\n- {name}.')
    return str

names = ['Phoebe', 'Rachel', 'Chandler']

#Say good morning to all
morning = greet('\nGood morning,', names)

#Say good afternoon to all
afternoon = greet('\nGood afternoon,', names)

#Say good night to all
night = greet('\nGood night,', names)

print (morning)
print (afternoon)
print (night)
Sign up to request clarification or add additional context in comments.

3 Comments

return can certainly be used more than once (and that’s fairly common). It can only be executed once.
Hi, first of all thank you for all the answers. In our code you have created a new variable (str) and put it equal to another variable (greeting). But couldn't we just assign the greeting to the rest? Or is it more advisable to create a new variable? Like this: greeting += (f'\n- {name}.'). Is this aproach not so good?
@DR8 Shouldn't be a problem, if you don't want greeting for anything else down the function.
1

The return statement also acts as a function terminator. So try print(greeting) instead of return(greeting) and also inside the loop. Final code:

def greet(greeting, names):
    #return a greeting and then the list of students
    print (greeting)
    for name in names:
        print (f'- {name}.')

names = ['Phoebe', 'Rachel', 'Chandler']

#Say good morning to all
greet('\nGood morning,', names)

#Say good afternoon to all
greet('\nGood afternoon,', names)

#Say good night to all
greet('\nGood night,', names)

Comments

1

I feel like this is what you're looking for:

def greet(greeting, names):
    #return a greeting and then the list of students
    print (greeting)
    for name in names:
        print ("- ", name)

names = ['Phoebe', 'Rachel', 'Chandler']

#Say good morning to all
morning = greet('\nGood morning,', names)

#Say good afternoon to all
afternoon = greet('\nGood afternoon,', names)

#Say good night to all
night = greet('\nGood night,', names)

1 Comment

For correctness, with this implementation, greet(...) doesn't return anything, so it's not necessary to assign variables to the non-existent return value. But it would generate the desired output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.