Day 3
#类和对象class Person:def __init__(self,name,sex,birthday):self.name=nameself.sex=sexself.birthday=birthdaydef say(self,word):print(f'{self.name}:“{word}”')
tom = Person('Tom','male','20200202')
cat = Person('Cat','male','20200202')
tom.say('hello')
cat.say('hi')
简单学生管理系统 v1.0.0
data=[{'id':111,'name':'Tom','sex':'man','add':'1 street'}]def show():for student in data:print(student)
def add():id=input('input id :')name=input('input name :')sex=input('input sex :')add=input('input add :')stu={'id':id,'name':name,'sex':sex,'add':add,}data.append(stu)def find():name=input('input name:')for stu in data:if stu['name'] == name:print(stu)returnelse:print('none')def alter():name = input('modify who ?:')for stu in data:if stu['name'] == name:print(stu)stu['name'] = input('input alter name:')stu['sex'] = input('input sex:')stu['add'] = input('input add:')else:print('none')def delete():name = input('input name:')for stu in data:if stu['name'] == name:print(stu)data.remove(stu)returnelse:print('none')while True:print("""
=======welcome to student management system=======1.show all student 2.add3.find4.alter5.delete0.exit """)op=input('input number:')if op == '1':show()elif op == '2':add()elif op == '3':find()elif op == '4':alter()elif op == '5':delete()elif op == '0':print('exit')break
v2
data=[
{'name':'Tom','sex':'male','add':'1street'},
{'name':'Cat','sex':'female','add':'2street'},
{'name':'Max','sex':'male','add':'3street'}
]
增加索引id
def v2(data_list):for index,stu in enumerate(data_list): #enumerate 可以拿到索引和值print(f'id:{index}', end='\t')print(f'name:{stu["name"]}',end='\t')print(f'sex:{stu["sex"]}',end='\t')print(f'add:{stu["add"]}')
选2 add的时候限制name不能为空
def input_name():while True:name = input('input name :').strip() #去掉字符串头尾空格if name:return nameelse:continue
限制性别只能男/女
def input_sex():while True:print('1(male) | 2(female)')n = input('choose sex:')if n == '1':return 'male'elif n == '2':return 'female'else:continue
添加 改了两个地方
def add():name=input_name()sex=input_sex()add=input('input add :')stu={'name':name,'sex':sex,'add':add,}data.append(stu)
查找
def find():name=input(' who ?:')for stu in data:if stu['name'] == name:print(stu)returnelse:print('none')
修改
def alter():name = input(' who ?:')for stu in data:if stu['name'] == name:print(stu)stu['name'] = input_name()stu['sex'] = input_sex()stu['add'] = input('input add:')returnelse:print('none')
删除
def delete():name = input('who ?:')for stu in data:if stu['name'] == name:print(stu)data.remove(stu) #删除returnelse:print('none')
最后调用的跟v1一样
day 4
class 类和对象
学生数据
data=[
{'name':'Tom','sex':'male','birth':'111111'},
{'name':'Cat','sex':'female','birth':'22222'},
{'name':'Cat','sex':'male','birth':'333333'}
]
学生类
初始化 方便后面调用self.name =传进来的name
class Student:def __init__(self,name,sex,birth):self.name=nameself.sex=sexself.birth=birth
#获取学生年龄def get_age(self):if self.birth:year=datetime.now().date().yearage=year-int(self.birth[:4])return ageelse:return 'none'
main函数作为程序的入口
与Java不同,python时从模块顶行开始执行,所以貌似不需要统一的main()作为入口
但是如果有部分代码不想在被导入到其他模块时被运行
则把代码放在if __name__=='__main__':
里
也就是说
#模块A
a=100
print('i am A‘)
if __name__=='__main__':print(a)
#模块B
from package1 import A
b=200
print('i am B‘)
print(b)输出
i am A
i am B
200
__name__
是Python的内置变量,用于指代当前模块
模块被直接运行时,__name__
的值就是__main__
导入了模块A,模块A中的__name__
的值是模块的真实名称package1.A
而模块B中是__main__
传name = bili_student_system 调用System类 中的start()
if __name__=='__main__':sys=System("bili_student_system") sys.start()
系统类
1、初始化 给self.name赋值 即 bili_student_system 一个空数组data[]
class System:def __init__(self,name):self.name = nameself.data=[]
启动start() 调用load_data() 和show_manu()和show()
def start(self):#系统启动时先加载数据self.load_data()while True:self.show_manu()op=input('choose number:')if op == '1':self.show()elif op == '2':self.add() elif op == '3':self.find()elif op == '4':self.modify()elif op =='5':self.remove()elif op == '0':print('exit')breakelse:print('please input right num :')
这里的data指一开始定义的学生数据 加到刚才定义的空数组
def load_data(self):for item in data:stu=Student(item['name'],item['sex'],item['birth'])self.data.append(stu)
显示菜单 #f-string {self.name}
def show_manu(self):print(f""" =======welcome to use {self.name}=======1.show all student 2.add3.find4.alter5.delete0.exit """)
1、显示所有学生信息 调用beauty() 美化打印
def show(self):self.beauty(self.data)
def beauty(self,data_list):for index,stu in enumerate(data_list):print(f'id:{index}',end='\t')print(f'name:{stu.name}',end='\t')print(f'sex:{stu.sex:2}', end='\t') #限制空间为2 对齐print(f'birth:{stu.get_age()}')
2、添加
def add(self):name=self.input_name()sex = self.choose_sex()birth = input('input birth:')stu=Student(name,sex,birth)self.data.append(stu)
限制名字不能为空
def input_name(self):while True:name = input('input name :').strip()if name :return nameelse:continue
性别男/女/未知
def choose_sex(self):sex=input('1(male|2 female)').strip()if sex == '1':return 'male'elif sex == '2':return 'female'else:return 'none'
3、查找
def find(self):find_list = self.find_name()
查找名字 以便反复使用
def find_name(self):name=self.input_name()find_list=[]for stu in self.data:if name.lower() in stu.name.lower():find_list.append(stu)if find_list:return find_listself.beauty(find_list)else:print(f'no find {name}')
希望输入小写也能查到 模糊匹配
4、修改学生信息
def modify(self):find_list = self.find_name()if find_list:# self.beauty(find_list)if len(find_list) > 1 :index=int(input('choose modify index:')) #输入的是字符串 要转换成整数stu = find_list[index]else:stu=find_list[0]print('now you modify :')self.beauty([stu]) #传列表name = input('modify name :').strip() #不调用input_name 是因为可以不改名字sex = self.choose_sex()birth = input('modify birth :')if name:stu.name=namestu.sex =sexstu.birth =birth
5、删除学生信息
def remove(self):find_list = self.find_name()if find_list:self.beauty(find_list)index = int(input('choose remove index:'))stu = find_list[index]print('now you remove :')self.beauty([stu]) #ctrl+shift+↑ 移动到上一行self.data.remove(stu)
快捷键
按住CTRL 鼠标左键点击函数名跳转
ctrl+shift+↑ 移动到上一行
完整
from datetime import datetime
data=[
{'name':'Tom','sex':'male','birth':'20100101'},
{'name':'Cat','sex':'female','birth':'20020202'},
{'name':'Cat','sex':'male','birth':'20050505'}]
#学生类
class Student:#初始化def __init__(self,name,sex,birth):self.name=nameself.sex=sexself.birth=birth#获取学生年龄def get_age(self):if self.birth:year=datetime.now().date().yearage=year-int(self.birth[:4])return ageelse:return 'none'#学生管理系统类
class System:#初始化def __init__(self,name):self.name = nameself.data=[]#美化输出def beauty(self,data_list):for index,stu in enumerate(data_list):print(f'id:{index}',end='\t')print(f'name:{stu.name}',end='\t')print(f'sex:{stu.sex:2}', end='\t')print(f'birth:{stu.get_age()}')#加载数据def load_data(self):for item in data:stu=Student(item['name'],item['sex'],item['birth'])self.data.append(stu)#启动系统def start(self):#系统启动时先加载数据self.load_data()while True:self.show_manu()op=input('choose number:')if op == '1':self.show()elif op == '2':self.add()elif op == '3':self.find()elif op == '4':self.modify()elif op =='5':self.remove()elif op == '0':print('exit')breakelse:print('please input right num :')#显示菜单def show_manu(self):print(f""" =======welcome to use {self.name}=======1.show2.add3.find4.modify5.delete0.exit """)#查找名字def find_name(self):name=self.input_name()find_list=[]for stu in self.data:if name.lower() in stu.name.lower():find_list.append(stu)if find_list:self.beauty(find_list)return find_listelse:print(f'no find {name}')#选择性别def choose_sex(self):sex=input('1(male|2 female) :').strip()if sex == '1':return 'male'elif sex == '2':return 'female'else:return 'none'#输入学生名字def input_name(self):while True:name = input('input name :').strip()if name :return nameelse:continue#1、显示所有学生信息def show(self):self.beauty(self.data)#2、添加学生信息def add(self):name=self.input_name()sex = self.choose_sex()birth = input('input birth:')stu=Student(name,sex,birth)self.data.append(stu)#3、查找学生信息def find(self):find_list = self.find_name()#4、修改学生信息def modify(self):find_list = self.find_name()if find_list:# self.beauty(find_list)if len(find_list) > 1 :index=int(input('choose modify index:')) #输入的是字符串 要转换成整数stu = find_list[index]else:stu=find_list[0]print('now you modify :')self.beauty([stu]) #传列表name = input('modify name :').strip() #不调用input_name 是因为可以不改名字sex = self.choose_sex()birth = input('modify birth :')if name:stu.name=namestu.sex =sexstu.birth =birth#5、删除学生信息def remove(self):find_list = self.find_name()if find_list:self.beauty(find_list)index = int(input('choose remove index:'))stu = find_list[index]print('now you remove :')self.beauty([stu]) #ctrl+shift+↑ 移动到上一行self.data.remove(stu)if __name__=='__main__':sys=System("bili_student_system")sys.start()