Python

[Mini Project] 파이썬 클래스 연습하기 | 클래스 기능을 활용한 학교 - 학급 - 학생관리 프로그램 (1st ~ 3rd try)

kyra 2022. 8. 15. 15:42

Project Idea:
- Make a program which allows a user to create a class, populate it with students, and query it.

Flow:
- The program should display a list of options to the user. These are: Creating a new class, deleting a class, teaching a class, removing a member from a class, adding a member to a class.
- The user should be able to create a class, enter a class name, and then fill it with as many students as they want. The students should have the following attributes: name, age, favourite subject
- The teach function should also take in a subject name. It should output the number of students that will be taught, and the names of the students whose favourite subject it is. 

Requirements:
- Everything should be class/function based
- User input should be validated 
- After an option has been completed, the program should always return to the main menu

Advice:
- Use a dictionary for the classes ("class name" - students)
- Use a class for the students
- For adding a member to a class and populating the class upon its creation, the same function could be used
- Removing a class and removing a member from a class may sound hard, but remember they are just removing an item from a collection

 

 


#define a basic Student class
class Student:
    name = ""
    age = ""
    favourite_subject = ""
    
    def setdata(self, name, age, favourite_subject):
        self.name = name
        self.age = age
        self.favourite_subject = favourite_subject


#create a new instance of the student class and define each attribute
student1 = Student()
student1.name = "A"
student1.age = 10
student1.favourite_subject = ["Math", "Science"]

student2 = Student()
student2.name = "B"
student2.age = 11
student2.favourite_subject = ["Math", "Art"]

student3 = Student()
student3.name = "C"
student3.age = 12
student3.favourite_subject = ["History", "P.E."]

def describe_student(student):
    return (f"{student.name} is {student.age} years old and likes {student.favourite_subject}.")

print(describe_student(student1))

1) Student 클래스에 넣을 Student info - name, age, favourite subject - 를 입력

2) Student 클래스를 통해 student1, student2, student3 객체를 만든다.

3) describe_student 메서드를 통해 제대로 입력이 되었는지 확인해본다.


Second try

 

#School Dictionary

School = {'Class A' : ['A', 'B'], 'Class B' : ['C', 'D']}

#make new classes or add new students to current classes
def add(class_name, student_name):
	if class_name in School.keys(): #만약 학급이 이미 존재한다면 
		School.get(class_name).append(student_name)  # 학급에 value 리스트에 학생 추가	
	else:  #학급이 존재하지 않으면
		School[class_name] = [student_name] #School 딕셔너리에 새로운 학급과 학생 추가

	print(School)


while True:
	question = input("Do you want to add students? (Y/N) : ")
	if question == 'Y':
		class_name = input('Enter the class name : ')
		student_name = input('Add students : ')
		add(class_name)
	elif question == 'N':
		print(f'School = {School}')
		break

 

1) 임의의 School 딕셔너리를 생성한다.

2) School 딕셔너리에 학급과 학생을 추가하는 함수 add를 만든다.

3) 학급 및 학생을 추가하는 질문을 while문으로 입력


 

Third try
class Student:
    name = ""
    age = ""
    favourite_subject = ""
    
    def setdata(self, name, age, favourite_subject):
        self.name = name
        self.age = age
        self.favourite_subject = favourite_subject

    def describe_student(self):
        return (f"{self.name} is {self.age} years old and likes {self.favourite_subject}.")
        
School = {'Class A' :[], 'Class B' : []}

def add_student(class_name, student):
    School.get(class_name).append(student) #여기서 student는 student = Student()라서, 입력받은 학생 정보 모두(이름, 나이, 과목)가 저장됨

def print_school():
    for class_name, students in School.items():
        print(f'{class_name} has : ')
        for student in students:
            print(student.describe_student()) #student클래스 내의 describe_student 메서드를 프린트

while True:
    name = input("Enter the student name : ")
    age = int(input("Enter the age : "))
    favourite_subject = input("Enter favourite subjects : ") #학생의 정보를 입력받기
    
    student = Student()   #Student클래스를 사용하는 student 객체 생성
    student.setdata(name, age, favourite_subject) #setdata 메서드를 활용, 입력받은 name, age, favourite_subject을 저장
    add_student('Class A', student) #입력받은 학생의 정보를 Class A에 저장
    print_school()

 

1)  그 전까지는 student1, student2, student3이 이미 지정되어 있었다면, 이번에는 저장할 학생의 이름, 나이, 과목 정보를 직접 사용자에게 입력받을 수 있게 input을 활용하여 name, age, favourite_subject를 받는다.

2) Student 클래스를 활용하는 student 객체를 만들고, Student 클래스 안 setdata 메서드를 활용해서 입력받은 정보를 저장한다.

3) add_student 함수를 활용해서 입력받은 학생의 정보를 'Class A'에 저장한다.

4) 정보가 잘 저장되어 있는지 출력하는 describe_student 메서드를 Student 클래스 안에 만들고, 이를 출력하는 print_school 함수를 만들어서 학교 현황을 출력할 수 있게 한다.

 


what needs to be done

1) Make it so we can say Yes/No to adding students
2) Make it so we can choose the class we add the sudents to
3) Deleting students