Gecko1994
Gecko1994

Reputation: 31

Python list generation from inputs

I recently began a Python course through PluralSight and one of th early modules on list creation and inputs has me a bit confused. I made the same code that the instructor presented but it's not acting as I think it should.

We were asked to take a code that asked for 2 inputs, a name and a student ID number, and add to it to create a program that asks for those 2 inputs and then ask if the user wants to add another name and number. If they do, it would ask for another name, number and if the user wants to add more. If the user says no, it should print out a list of names (but not numbers).

I came up with a code that does that, but when I print out the list, it only prints out the most recent name that was input. I can't figure out how to print a list of multiple names, any advice? Thanks!

students = []

def get_students_titlecase():
    students_titlecase = []
    for student in students:
        students_titlecase = student["name"].title()
    return students_titlecase

def print_students_titlecase():
    students_titlecase = get_students_titlecase()
    print(students_titlecase)

def add_student(name, student_id=332):
    student = {"name": name, "student_id": student_id}
    students.append(student)


student_list = get_students_titlecase()



student_name = input("Enter Student name: ")
student_id = input("Enter Student id: ")
add_student(student_name, student_id)
add_more = input("Add another student? [y/n]: ")


while add_more == "y":
    student_name = input("Enter Student name: ")
    student_id = input("Enter Student id: ")
    add_student(student_name, student_id)
    add_more = input("Add another student? [y/n]: ")


if add_more == "n":
    print_students_titlecase()

Upvotes: 3

Views: 101

Answers (1)

itdoesntwork
itdoesntwork

Reputation: 4802

Look at the third line of get_students_titlecase. You should be appending to the array, not setting it to a value.

students_titlecase.append(student["name"].title())

Upvotes: 3

Related Questions