- 0.定義?個(gè)學(xué)?類墓阀。有屬性:姓名禾乘、年齡惋耙、成績(jī)(語?,數(shù)學(xué)浇揩,英語)[每課成績(jī)的類型為整數(shù)]
?法: a. 獲取學(xué)?的姓名: getname() b. 獲取學(xué)?的年齡: getage()
c. 返回3?科?中最?的分?jǐn)?shù)。 get_course()
class Student:
def __init__(self,name,age,chinese_score,math_score,english_score):
self.name = name
self.age = age
self.chinese_score = chinese_score
self.math_score = math_score
self.english_score = english_score
def get_name(self):
print(self.name)
def get_age(self):
print(self.age)
def get_course(self):
lists = [self.chinese_score,self.math_score,self.english_score]
print(max(lists))
stu1 = Student('xzq',18,89,90,10)
stu1.get_name()
stu1.get_age()
stu1.get_course()
D:\千鋒培訓(xùn)作業(yè)\第三周作業(yè)\day14-作業(yè)\venv\Scripts\python.exe D:/千鋒培訓(xùn)作業(yè)/第三周作業(yè)/day14-作業(yè)/homework/作業(yè)0.py
xzq
18
90
Process finished with exit code 0
- 1.建??個(gè)汽?類Auto憨颠,包括輪胎個(gè)數(shù)胳徽,汽?顏?积锅,?身重量,速度等成員變量养盗,并通過不同的構(gòu)造?法創(chuàng)建實(shí)例缚陷。?少要求 汽?能夠加速 減速 停?。 再定義?個(gè)?汽?類CarAuto 繼承Auto 并添加空調(diào)往核、 CD等成
員變量 覆蓋加速 減速的?法
class Auto:
def __init__(self,tyre='',color='',weight='',speed=''):
self.tyre = tyre
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self):
print('汽車正在加速箫爷!')
def speed_down(self):
print('汽車正在減速!')
def stop(self):
print('汽車已停車聂儒!')
class CarAuto(Auto):
def __init__(self,air_conditioner='',CD=''):
super().__init__(tyre='',color='',weight='',speed='')
self.air_conditioner = air_conditioner
self.CD = CD
def speed_up(self):
print('汽車正在加速虎锚!')
def speed_down(self):
print('汽車正在減速!')
def stop(self):
print('汽車已停車衩婚!')
car1 = CarAuto(10,20)
car1.tyre = 11
print(car1.tyre)
D:\千鋒培訓(xùn)作業(yè)\第三周作業(yè)\day14-作業(yè)\venv\Scripts\python.exe D:/千鋒培訓(xùn)作業(yè)/第三周作業(yè)/day14-作業(yè)/homework/作業(yè)1.py
11
Process finished with exit code 0
- 2.創(chuàng)建?個(gè)名為User 的類窜护,其中包含屬性firstname 和lastname ,還有?戶簡(jiǎn)介通常會(huì)存儲(chǔ)的其他?個(gè)屬性非春。在類User 中定義?個(gè)名 為describeuser() 的?法柱徙,它打印?戶信息摘要;再定義?個(gè)名為greetuser() 的?法,它向?戶發(fā)出個(gè)性化的問候奇昙。
管理員是?種特殊的?戶护侮。編寫?個(gè)名為Admin 的類,讓它繼承User類储耐。添加?個(gè)名為privileges 的屬性概行,?于存儲(chǔ)?個(gè)由字符串(如"can add post"、 "can delete post"弧岳、 "can ban user"等)組成的列表凳忙。編寫?個(gè)名為
show_privileges()的?法,它顯示管理員的權(quán)限禽炬。創(chuàng)建?個(gè)Admin 實(shí)例涧卵,并調(diào)?這個(gè)?法。
class User:
def __init__(self,firstname,lastname):
self.firstname = firstname
self.lastname = lastname
def describeuser(self):
print('用戶信息:%s %s'% (self.firstname,self.lastname))
def greetuser(self):
print('%s你好啊'% self.lastname)
class Admin(User):
def __init__(self,privileges=[]):
super().__init__(firstname='',lastname='')
self.privileges = []
def show(self):
self.__show_privileges()
@classmethod
def __show_privileges(cls):
print('管理員腹尖,你好')
ad1 = Admin('i can do it')
ad1.show()
D:\千鋒培訓(xùn)作業(yè)\第三周作業(yè)\day14-作業(yè)\venv\Scripts\python.exe D:/千鋒培訓(xùn)作業(yè)/第三周作業(yè)/day14-作業(yè)/homework/作業(yè)2.py
管理員柳恐,你好
Process finished with exit code 0
- 3.創(chuàng)建?個(gè)Person類,添加?個(gè)類字段?來統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Person:
number = 0
def __init__(self):
Person.number += 1
@classmethod
def numbers(cls):
return cls.number
a =Person()
b =Person()
c =Person()
print(Person.numbers())
D:\千鋒培訓(xùn)作業(yè)\第三周作業(yè)\day14-作業(yè)\venv\Scripts\python.exe D:/千鋒培訓(xùn)作業(yè)/第三周作業(yè)/day14-作業(yè)/homework/作業(yè)3.py
3
Process finished with exit code 0
- (嘗試)5.寫?個(gè)類热幔,其功能是: 1.解析指定的歌詞?件的內(nèi)容 2.按時(shí)間顯示歌詞 提示:歌詞?件的內(nèi)容?般
是按下?的格式進(jìn)?存儲(chǔ)的乐设。歌詞前?對(duì)應(yīng)的是時(shí)間,在對(duì)應(yīng)的時(shí)間點(diǎn)可以顯示對(duì)應(yīng)的歌詞
[00:00.20]藍(lán)蓮花
[00:00.80]沒有什么能夠阻擋
[00:06.53]你對(duì)?由地向往
[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]當(dāng)你低頭地瞬間
[02:26.79][02:05.72][00:37.16]才發(fā)覺腳下的路
[02:32.17][00:42.69]?中那?由地世界
[02:37.20][00:47.58]如此的清澈?遠(yuǎn)
[02:42.32][00:52.72]盛開著永不凋零
[02:47.83][00:57.47]藍(lán)蓮花
import re
class Lyrics:
"""歌詞類"""
def __init__(self):
self.lrc = ''
self._time = 0
@property
def time(self):
return self._time
@time.setter
def time(self,time):
fen = float(time[:2])
miao = float(time[3:])
self._time = fen*60 + miao
def __lt__(self, other):
return self._time < other.time
#自定義類的對(duì)象的打印
def __str__(self):
return str(self._time) + ':' + self.lrc
class AnalysisLyrcs:
"""解析歌詞"""
def __init__(self,file_path):
self.all_lrc = []
self.file_path = file_path
def __anlysis_lyrics(self):
"""解析歌詞"""
with open(self.file_path,'r',encoding='utf-8')as f:
#一行一行的讀文件中的內(nèi)容
line = f.readline()
while line:
#對(duì)當(dāng)前讀的一行的內(nèi)容進(jìn)行操作
self.__operate_line(line)
line = f.readline()
# 在這人已經(jīng)將文件中所有的內(nèi)容都讀完了绎巨,并且轉(zhuǎn)換成了歌詞對(duì)象近尚,保存到all_lrc中了
# 排序
self.all_lrc.sort(reverse=True)
# for lrc in self.all_lrc:
# print(lrc)
def __operate_line(self,line):
# print(line)
# [00:00.20]藍(lán)蓮花
#切割
lines = line.split(']')
# ['00:00.20','藍(lán)蓮花']
#取詞
lrc = lines[-1]
#遍歷所有的時(shí)間
for index in range(len(lines)-1):
# print('%s:%s'%(lrc , lines[index]))
# 創(chuàng)建歌詞對(duì)象
lyrics = Lyrics()
# 設(shè)置歌詞和時(shí)間屬性
lyrics.lrc = lrc
lyrics.time = lines[index][1:]
# 00:00.20
# 保存歌詞對(duì)象
self.all_lrc.append(lyrics)
def show_lrc(self,time):
if not self.all_lrc:
self.__anlysis_lyrics()
self.__anlysis_lyrics()
for item in self.all_lrc:
if item.time <time:
return item.lrc
# lan_lian_hua = AnalysisLyrcs('./藍(lán)蓮花')
# lan_lian_hua.anlysis_lyrics()
lan_lian_hua = AnalysisLyrcs('./藍(lán)蓮花')
# print(lan_lian_hua.show_lrc(100))
# print(lan_lian_hua.show_lrc(15))
import time
time1 = 0
while True:
time.sleep(1)
time1 += 1
print(lan_lian_hua.show_lrc(time1))