简体   繁体   中英

How can I fix a bug that output is repeated the same

I am trying to create a student mark list. Firstly, i have to create both student and course list. However, i'm stuck at this problem. Here is my code:

from datetime import datetime

class StudentMark():
    class Student():
        def studentCount(student):
            student.count = int (input ("Enter number of students: "))
            return student.count

        def studentDetail(student):
            student.sID = input ("Student id: ")
            student.sName = input ("Student name: ")
            student.sDOB = input("Student's DOB:")
            student.setDOB=datetime.strptime(student.sDOB, "%d/%m/%Y")
            return student.sID, student.sName, student.setDOB
            
        def studentList(student):
            sList = []
            for i in range(student.count):
                sList.append ((student.sID, student.sName, student.setDOB))
                sList.sort()       
            for s in sList:
                print (f"Student id: {s[0]} Name: {s[1]} Date of birth: {s[2]}")      
            
    class Course():
        def courseCount(course):
            course.count = int (input ("Enter number of courses: "))
            return course.count

        def courseDetail(course):
            course.cID = input ("Course id: ")
            course.cName = input ("Course name: ")
            return course.cID, course.cName
            
        def courseList(course):
            cList = []
            for i in range(course.count):
                cList.append ((course.cID, course.cName))
                cList.sort()       
            for c in cList:
                print (f"Course id: {c[0]} Name: {c[1]}")                

    

    
    std = Student()
    std.studentCount()
    std.studentDetail()
    std.studentList()
    
    crs = Course()
    crs.courseCount()
    crs.courseDetail()
    crs.courseList()

When i want to input 2 and more than 2 datas. For example:

Enter number of students: 2
Student id: 111
Student name: max
Student's DOB:11/11/1111
Student id: 111 Name: max Date of birth: 1111-11-11 00:00:00
Student id: 111 Name: max Date of birth: 1111-11-11 00:00:00
Enter number of courses: 2
Course id: 1
Course name: math
Course id: 1 Name: math
Course id: 1 Name: math

As you can see, the outputs are repeated and i don't know how to fix this problem. Hope everyone can help me how to solve it.

The first for loop inside the method studentList() is looping twice, as the range(student.count) is evaluated to 2. Notice that the student.count got the value informed at the first input('Enter number of students:') , which was 2.

Besides the twice loop, the values being added to the list inside the loop doesn't change, as they refer to properties of the same instance of the object student:

def studentList(student):
    sList = []
    for i in range(student.count):
        sList.append ((student.sID, student.sName, student.setDOB))
        sList.sort()       
    for s in sList:
        print (f"Student id: {s[0]} Name: {s[1]} Date of birth: {s[2]}")   

To achieve the effect of collecting more than one Student and print them all, you must store each Student in an array right after you have collected the student's data. Like this:

from datetime import datetime
import copy

class StudentMark():
    class Student():
        students = []

        def studentCount(student):
            student.count = int (input ("Enter number of students: "))
            return student.count

        def studentDetail(student):
            for s in range(student.count):
                print(f"### Student {s+1} entry ###")
                student.sID = input ("Student id: ")
                student.sName = input ("Student name: ")
                student.sDOB = input("Student's DOB: ")
                student.setDOB=datetime.strptime(student.sDOB, "%d/%m/%Y")
                student.students.append(copy.copy(student))
            
        def studentList(student):
            for s in student.students:
                print (f"Student id: {s.sID} Name: {s.sName} Date of birth: {s.sDOB}")

I've tried to fix the code with minimum effort. There are better ways to achieve this result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM