[SOLVED] How can I return multiple values from a function?

Issue

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?

Solution

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)

Answered By – TheMaster

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.