Skip to content
Advertisement

Is there a way to tranfer data from a function into a list?

I keep on trying to get the data from the function to display in the list, I’ve tried using list.append but I keep on getting errors, what am I doing wrong?

def pick_up():
        name=input("Good Afternoon, who would you like to pick up? ")
        additional_student=input(f"Is {name} the only person you want to pick up ?YES/NO")
        if additional_student == "Yes":
            print(f"Please wait, {name} would be with you shortly")
        else:
            name_2=input("Who else would you like to pick? ")
            print(f"Please wait, {name} and {name_2} would be with you shortly.")

pick = pick_up()
picked_students.append(pick)
print(picked_students)
pick_up()

Advertisement

Answer

Your pick_up() function needs to return something. Since it can return either one or two students, you probably want it to return a list of either one or two students, and extend your picked_students list with that list.

def pick_up():
    name=input("Good Afternoon, who would you like to pick up? ")
    additional_student=input(f"Is {name} the only person you want to pick up ?YES/NO")
    if additional_student == "Yes":
        print(f"Please wait, {name} would be with you shortly")
        return [name]
    
    name_2=input("Who else would you like to pick? ")
    print(f"Please wait, {name} and {name_2} would be with you shortly.")
    return [name, name_2]

picked_students = []
pick = pick_up()
picked_students.extend(pick)
print(picked_students)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement