

python3 に関する質問です  
僕は今学校の課題としてpythonで"学校の入学の登録"のプログラミングをしているのですが  
以下のリストの内のStudentGender,StudentBirthday,StudentNationalityのリストの要素の数が勝手に増えるという問題があります。  
処理中のリストを呼び出して何処に問題があるかは確認できたのですがそこからどうやって解決していくのか（具体的に言うとStudentGender,StudentBirthday,StudentNationalityの要素の数をそのまま（増えたりせずに）StudentListの順に並べる）よくわからないので解決策を教えてくれると幸いです。

[code]

    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)):#恐らくここに問題があります
            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:
            welcoming = True
        else :
            welcoming = False
    
        return welcoming,NoofStudent
    
    def Appologize():
        print("Sorry, But our enrolling is finished.")
    
    def OutputList(StudentList,StudentGender,StudentBirthday,StudentNationality,NoofStudent):
        for j in 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. :"))
    welcoming = True
    if welcoming == True:
        while welcoming == True:
            Namelist,Genderlist,Birthdaylist,Nationallist = EnterStudentInformation(LowerGradeLimit)
            StudentList,StudentGender,StudentBirthday,StudentNationality = Sortenrollinglist(Namelist,Genderlist,Birthdaylist,Nationallist)
            welcoming,NoofStudent = CheckEnrollingList(Enrollinglimit,StudentList)
            OutputList(StudentList,StudentGender,StudentBirthday,StudentNationality,NoofStudent)
    elif welcoming == False:
        Appologize()
    OutputList(StudentList,StudentGender,StudentBirthday,StudentNationality,NoofStudent)
    
[/code]

[![スクリーンショット](https://i.stack.imgur.com/SC1Xr.png)](https://i.stack.imgur.com/SC1Xr.png)


CC BY-SA 4.0
2020-01-17T13:54:55.100
62330
2020-01-17T22:55:24.367
2020-01-17T22:55:24.367
19110
36859
question
python python3
Python のリストの要素の数が勝手に増える


for ループの前に、配列を空にする必要があります。

[code]

    def Sortenrollinglist(Namelist,Genderlist,Birthdaylist,Nationallist):
        StudentList = sorted(Namelist)
        print(StudentList)
    
        # 以下3行を追加
        StudentGender = []
        StudentBirthday = []
        StudentNationality = []
    
        for i in range (0,len(Namelist)):#恐らくここに問題があります
            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)
    
[/code]

* * *

また、このソートは何度も行う必要はなく、最後に1回行えば十分ではないでしょうか？十分なら while ループの外で実行すれば良いでしょう。


CC BY-SA 4.0
2020-01-17T16:40:10.027
62333
2020-01-17T16:40:10.027
32959
62330
answer