Search code examples
pythonpython-3.xlistnested-loopsnested-lists

creating diploma using nested lists ( Python )


my collage professor asked to make a diploma that have 2 lists [coruses and students] and nest in it 5 groups in Each group 5 students and fill their names in it Each student have 5 infos(any) all using nested lists.. but later on said do it with anything you could do it with.

i tried to do it in lists and reached this if i appended any input the hole programming goes full of errors

is it possiable in the first place ?

diploma = []
std = []
less_than_15 = []
for i in range(5):
    std.append(list(str(i)))
    std[i].remove(str(i))
    std[i].append(list(str(i)))
    std[i][0].remove(str(i))
    std[i][0].append(list(str(i)))
    std[i][0][0].remove(str(i))


Solution

  • Your code won't work the way you expect it to work because of the following reasons:

    You are creating a list of strings instead of a list of lists for each student and group. You are removing and appending elements to the lists in a confusing and unnecessary way. You are not filling the names and infos of the students as required by the problem statement

    I'll write a code for you.

    for what I figured out.

    courses = [] 
    students = (5,5,5) # it has 5 rows, each with 5 sub-rows, each with 5 elements
    

    example:

    you can fill it like this:

    student = [
            #first group
        [
            ["student1", 14, "male", "B-", "math"],
            [                                    ],
                    [                                    ],
                    [                                    ],
                    [                                    ]
            ],
    
            #second group
            and so on
             
    
          ]
    

    here is all the code you need

    for i in range(len(students)):
        print(f"Group {i+1}:")
        for j in range(len(students[i])):
            print(f"Student {j+1}: {', '.join(str(x) for x in students[i][j])}")
    

    another way to implement the same code is to use random python library:

    here is the code

    import random
    
    # Define the courses list
    courses = ["Math", "Science", "English", "History", "Art"]
    
    # Define some sample names and infos
    names = ["Alice", "Bob", "Charlie","Paul", "Quinn"]
    infos = ["Age: 18", "Gender: Female", "Grade: A+", "Course: Math", "Hobby: Painting"]
    
    # Create an empty list for students
    students = []
    
    # Loop over 5 groups
    for i in range(5):
        # Create an empty list for each group
        group = []
        # Loop over 5 students in each group
        for j in range(5):
            # Create an empty list for each student
            student = []
            # Append a random name to the student list
            student.append(random.choice(names))
            # Loop over 4 infos for each student
            for k in range(4):
                # Append a random info to the student list
                student.append(random.choice(infos))
            # Append the student list to the group list
            group.append(student)
        # Append the group list to the students list
        students.append(group)
    
    # Print the diploma
    print("Diploma")
    print("Courses: ", ", ".join(courses))
    print("Students:")
    for i in range(len(students)):
        print(f"Group {i+1}:")
        for j in range(len(students[i])):
            print(f"Student {j+1}: {', '.join(str(x) for x in students[i][j])}")