1. 程式人生 > >python中利用類創建的對象來保存信息

python中利用類創建的對象來保存信息

bsp 保存信息 ngs sha 入學 env exe pre info

在類創建的對象中,一般都是以字典的方式來class Student:

class Student:
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score


def input_student_info():
    l = []
    while True:
        n = input("請輸入學生姓名:")
        if n == "":
            break
        a 
= input("請輸入學生年齡:") s = input("請輸入學生分數:") l.append((Student(n, a, s)).__dict__) print(l) input_student_info()

輸出結果:

D:\program\pycharm\student_init\venv\Scripts\python.exe D:/program/pycharm/student_init/student.py
請輸入學生姓名:zengsf
請輸入學生年齡:20
請輸入學生分數:90
請輸入學生姓名:fengshao
請輸入學生年齡:23
請輸入學生分數:80
請輸入學生姓名:
[{‘name‘: ‘zengsf‘, ‘age‘: ‘20‘, ‘score‘: ‘90‘}, {‘name‘: ‘fengshao‘, ‘age‘: ‘23‘, ‘score‘: ‘80‘}]

python中利用類創建的對象來保存信息