定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法: a. 获取学生的姓名:getname() b. 获取学生的年龄:getage()
c. 返回3门科目中最高的分数。get_course()
class Student:
def __init__(self, name, age, *score):
self.name = name
self.age = age
self.score = score
def getname(self):
print(self.name)
def getage(self):
print(self.age)
def get_course(self):
print(max(self.score))
student1 = Student('老王', 20, 98, 85, 62)
student1.getname()
student1.getage()
student1.get_course()
创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self):
Person.count += 1
for x in range(100):
x = Person()
print(Person.count) # 打印的100
写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
[00:00.20]蓝莲花
[00:00.80]没有什么能够阻挡
[00:06.53]你对自由地向往
[00:11.59]天马行空的生涯
[00:16.53]你的心了无牵挂
[02:11.27][01:50.22][00:21.95]穿过幽暗地岁月
[02:16.51][01:55.46][00:26.83]也曾感到彷徨
[02:21.81][02:00.60][00:32.30]当你低头地瞬间
[02:26.79][02:05.72][00:37.16]才发觉脚下的路
[02:32.17][00:42.69]心中那自由地世界
[02:37.20][00:47.58]如此的清澈高远
[02:42.32][00:52.72]盛开着永不凋零
[02:47.83][00:57.47]蓝莲花
class LyricsParsing:
lyrics_dictionary = {}
sum1 = []
new_dic = {}
def __init__(self, name):
self.name = name
self.read_files()
@staticmethod
def change(string):
string1 = string[0:2]
string2 = string[3:]
if string1.startswith('0'):
string1 = string1[1:]
minute = int(string1) * 60
second = float(string2)
time = minute + second
return time
@staticmethod
def extract(string, flag=True):
global lyric
if flag:
opposite_string = string[::-1]
for x in range(len(opposite_string)):
if opposite_string[x] == ']':
lyric = opposite_string[0:x]
lyric = lyric[::-1]
break
if string.startswith('['):
string1 = string[1:9]
string2 = string[10:]
string1 = LyricsParsing.change(string1)
LyricsParsing.lyrics_dictionary[string1] = lyric
LyricsParsing.extract(string2, False)
def read_files(self):
url = './files/' + self.name + '.txt'
with open(url, 'r', encoding='utf-8') as f:
while True:
string = f.readline().strip('\n')
if not string:
break
LyricsParsing.extract(string)
@staticmethod
def sort_key():
for key in LyricsParsing.lyrics_dictionary:
LyricsParsing.sum1.append(key)
LyricsParsing.sum1.sort()
@staticmethod
def sort_dict():
LyricsParsing.new_dic[LyricsParsing.sum1[0]] = '没有歌词'
for x in range(len(LyricsParsing.sum1)-1):
value = LyricsParsing.lyrics_dictionary.get(LyricsParsing.sum1[x])
LyricsParsing.new_dic[LyricsParsing.sum1[x+1]] = value
else:
LyricsParsing.new_dic[300] = LyricsParsing.lyrics_dictionary.get(LyricsParsing.sum1[-1])
def find(self):
self.read_files()
LyricsParsing.sort_key()
LyricsParsing.sort_dict()
time = float(input('请输入时间(单位:秒):'))
for x in LyricsParsing.new_dic:
if x > time:
print(LyricsParsing.new_dic[x])
break
song1 = LyricsParsing('蓝莲花')
song1.find()