Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list generation from inputs

Tags:

python

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()
like image 597
Gecko1994 Avatar asked Dec 02 '25 06:12

Gecko1994


1 Answers

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())
like image 50
itdoesntwork Avatar answered Dec 03 '25 19:12

itdoesntwork



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!