class Person(object):
'''
定义校长类、教师类、学生类的基类 Person。
'''
def __init__(self, name, sex):
self.name = name
self.sex = sex
class President(Person):
'''
定义校长类,该类继承自 Person 类。
'''
schools = []
def __init__(self, name, sex):
super().__init__(name, sex)
def create_school(self,school_name,school_address):
'''
创建课程
:school_name:学校名称;
:school_address:学校地址;
:return: School。
'''
school = School(school_name,school_address)
President.schools.append(school)
print(f'{self.name} 创建了学校:{school.name}!')
return school
def create_course(self,course_name):
course = Course(course_name)
print(f'{self.name} 创建了课程:{course.name}!')
return course
def create_teacher(self,teacher_name,teacher_password):
teacher = Teacher(teacher_name,teacher_password)
print(f'{self.name} 招聘老师:{teacher.name}!')
return teacher
class Teacher(Person):
'''
定义教师类,该类继承自 Person 类。
'''
def __init__(self,name,sex):
super().__init__(name,sex)
self.courses = []
def add_course(self,course):
self.courses.append(course)
print(f'{self.name} 增加了 {course.name}')
def scoring(self, student, course, record:float):
name = course.name
for v in student.courses:
if v.name == name:
v.record = record
print(f'{self.name} 老师给 {student.name} 的 {v.name} 课程打了 {record} 分!')
return
print(f'{self.name} 老师发现学生 {student.name} 没有购买 {v.name} 课程!')
class Student(Person):
'''
定义学生类,该类继承自 Person 类。
'''
def __init__(self,name,sex):
super().__init__(name,sex)
self.courses = []
self.school = ''
def get_school(self, president):
for v in president.schools:
print(f'学生 {self.name} 所在学校为:{v.name}!')
def choice_school(self,school):
self.school = school
print(f'{self.name} 选择了 {school.name}!')
def choice_course(self,course):
if course.name in self.school.courses:
self.courses.append(course.copy())
course.students.append(self.name)
print(f'{self.name} 选修了 {course.name} 课程!')
else:
print(f'{self.school.name} 没有选修 {course.name} 课程!')
class School(object):
'''学校类'''
def __init__(self,name,address):
self.name = name
self.address = address
self.courses = []
def add_course(self,course):
course_name = course.name
self.courses.append(course_name)
print(f'{self.name} 学校增加了 {course_name} 课程!')
import copy
class Course(object):
'''课程类'''
def __init__(self, name):
self.name = name
self.record = 0
self.students = []
def add_student(self, student):
self.students.append(student)
print(f'{student} 选修了 {self.name} 课程!')
def copy(self):
return copy.copy(self)
# 校长
president = President('校长','男')
# 校长创建了school
school = president.create_school('陵水道小学','河西区')
# 校长创建了课程
python = president.create_course('Python')
linux = president.create_course('Linux')
# 学校增加课程
school.add_course(python)
school.add_course(linux)
# 校长招聘老师
tom = president.create_teacher('Tom','男')
mary = president.create_teacher('Mary','女')
# 学生
print()
students = []
zs = Student('ZS','男')
zs.get_school(president)
zs.choice_school(school)
zs.choice_course(python)
zs.choice_course(linux)
students.append(zs)
print()
ls = Student('LS','男')
ls.get_school(president)
ls.choice_school(school)
ls.choice_course(python)
ls.choice_course(linux)
students.append(ls)
# 老师给学生打分
print()
tom.scoring(zs,python,100)
tom.scoring(ls,python,90)
print()
mary.scoring(zs,linux,75)
mary.scoring(ls,linux,80)
# 打印学生的课程信息
print("\n打印所有学生的课程信息:")
for student in students:
for course in student.courses:
print(f' {student.name} 的 {course.name} 课程得分为:{course.record}!')
# 1、已知:
# para='''The actress died at age 83 on December 1, 2008
# from heart failure after a brief illness. She was best
# known for playing maid Rosario Salazar on Will & Grace
# from 1999 to 2006.'''
# 使用 Python 正则表达式从 para 中:
import re
para = '''The actress died at age 83 on December 1, 2008
from heart failure after a brief illness. She was best
known for playing maid Rosario Salazar on Will & Grace
from 1999 to 2006.'''
#(1)提取所有字符
pattern = r'.'
result = re.findall(pattern, para)
print(result)
#(2)仅提取所有字符
pattern = r'\w'
result = re.findall(pattern, para)
print(result)
#(3)仅提取数字
pattern = r'\d'
result = re.findall(pattern, para)
print(result)
#(4)提取所有单词和数字
pattern = r'\w+'
result = re.findall(pattern, para)
print(result)
#(5)仅提取数字
pattern = r'\d+'
result = re.findall(pattern, para)
print(result)
#(6)提取开头单词
pattern = r'^\w+'
result = re.findall(pattern, para)
print(result)
#(7)从每个单词中提取前两个字符(而不是数字)
pattern = r'\b[a-zA-Z].'
result = re.findall(pattern, para)
print(result)
#(8)找出以元音开头的所有单词
pattern = r'\b[aeiou]\w+'
result = re.findall(pattern, para, re.IGNORECASE)
print(result)
#(9)找出所有以辅音开头的单词
pattern = r'\b[^aeiou0-9 ]\w+'
result = re.findall(pattern, para, re.IGNORECASE)
print(result)
#(10)计算 a、an 和 the 的总数
pattern = r'the|a|an'
result = re.findall(pattern, para, re.IGNORECASE)
print(len(result))
#(11)提取日期
pattern = r'(\w+)(\s)(\d+)([,]\s)(\d+)'
result = re.findall(pattern, para)
found = []
for v in result:
found.append("-".join([v[0],v[2],v[4]]))
print(found)
# 2、编写函数判断函数输入是否包含汉字。
def is_hanzi(value):
"""
检查整个字符串是否包含汉字
:param value: 需要检查的字符串
:return: bool
"""
for ch in value:
if re.match(u"[\u4e00-\u9fa5]",ch):
return True
return False
print(is_hanzi("Hello world!"))
# 3、编写函数判断函数输入是否包含中文字符或中文符号。
def is_chinese(value):
"""
检查整个字符串是否包含中文或中文字符
:param value: 需要检查的字符串
:return: bool
"""
for ch in value:
if re.match(u"[^\x00-\xff]",ch):
return True
return False
|