The number of elements in Python's list increases on its own.

Asked 2 years ago, Updated 2 years ago, 31 views

Questions about python 3
I am currently programming "Registration of School Entrance" on python as a school assignment.
The problem is that the number of elements in the StudentGender, StudentBirthday, StudentNationality list in the list below will increase on its own.
I was able to call the list I was working on and see where the problem was, but I am not sure how to solve it (more specifically, StudentGender, StudentBirthday, StudentNationality elements in order of StudentList).

import datetime

Namelist=[ ]
Genderlist = [ ]
Birthdaylist = [ ]
Nationallist = [ ]

StudentList= [ ]
StudentGender= [ ]
StudentBirthday= [ ]
StudentNationality= [ ]


def datainput():
    print("Enter your Birthday")
    y1 = int(input("Enter the year of your birthday.":")
    m1 = int(input("Enter the month of your birthday.":")
    d1 = int(input("Enter the day of your birthday.":")
    dt1 = datetime.date (year=y1, month=m1, day=d1)
    dt2 = "{0:%Y /%m /%d}".format(dt1)
    Btd = str(dt2)
    print("")
    print("Your birthday is", Btd, "."")
    print("")
    return dt1,Btd

def EnterStudentInformation (LowerGradeLimit):
    Grade=int(input("Enter Your Grade:")
    if Grade<LowerGradeLimit:
        print("You can't enter this school.")
    else:
        Name = str(input("Enter Your Name.:")
        Gender=str(input("Enter Your Gender.":")
        dt1,Btd = datainput()
        Nationality=str(input("Enter Your Nationality.":")
        Namelist.append(Name)
        Genderlist.append (Gender)
        Birthdaylist.append(Btd)
        Nationallist.append (Nationality)
        print (Namelist)
        print(Genderlist)
        print(Birthdaylist)
        print (Nationallist)

    return Namelist, Genderlist, Birthdaylist, Nationallist

def Sortenrollinglist (Namelist, Genderlist, Birthdaylist, Nationallist):
    StudentList=sorted (Namelist)
    print(StudentList)
    for i in range(0,len(Namelist)): # Maybe this is the problem
        StudentGender.append (Genderlist [Namelist.index(StudentList[i])])
        StudentBirthday.append (Birthdaylist [Namelist.index(StudentList[i])])
        StudentNationality.append (Nationallist [Namelist.index(StudentList[i])])
    print(StudentGender)
    print(StudentBirthday)
    print(StudentNationality)

    return StudentList, StudentGender, StudentBirthday, StudentNationality

def CheckEnrollingList (Enrollinglimit, StudentList):
    NoofStudent=len (StudentList)
    if NoofStudent<Enrollinglimit:
        welcome=True
    else:
        welcome = False

    return welcome, NoofStudent

def Appologize():
    print("Sorry, but your enrolling is finished.")

def OutputList (StudentList, StudentGender, StudentBirthday, StudentNationality, NoofStudent):
    for jin range (NoofStudent):
        print(StudentList[j], "", StudentGender[j], "", StudentBirthday[j], "", StudentNationality[j])
        print("")


Enrollinglimit=int(input("Enter student limitation for enrolling school.":")
LowerGradeLimit=int(input("Enter lower grade limitation for enrolling school.:")
welcome=True
if welcome == True:
    while welcome == True:
        Namelist, Genderlist, Birthdaylist, Nationallist= EnterStudentInformation (LowerGradeLimit)
        StudentList, StudentGender, StudentBirthday, StudentNationality=Sortenrollinglist (Namelist, Genderlist, Birthdaylist, Nationallist)
        welcome, NoofStudent=CheckEnrollingList (Enrollinglimit, StudentList)
        OutputList (StudentList, StudentGender, StudentBirthday, StudentNationality, NoofStudent)
elif welcome==False:
    Appologize()
OutputList (StudentList, StudentGender, StudentBirthday, StudentNationality, NoofStudent)

Screenshot

python python3

2022-09-29 22:50

1 Answers

The array must be empty before the for loop.

def Sortenrollinglist (Namelist, Genderlist, Birthdaylist, Nationallist):
    StudentList=sorted (Namelist)
    print(StudentList)

    # Add the following 3 lines
    StudentGender= [ ]
    StudentBirthday= [ ]
    StudentNationality= [ ]

    for i in range(0,len(Namelist)): # Maybe this is the problem
        StudentGender.append (Genderlist [Namelist.index(StudentList[i])])
        StudentBirthday.append (Birthdaylist [Namelist.index(StudentList[i])])
        StudentNationality.append (Nationallist [Namelist.index(StudentList[i])])
    print(StudentGender)
    print(StudentBirthday)
    print(StudentNationality)

Also, isn't it necessary to do this sort over and over again, but should it be enough to do it once at the end?If it's enough, you can run it outside the while loop.


2022-09-29 22:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.