python基础入门升级版 第4课练习及作业 机器龙007


题目1:创建一个名为phone的类
1)类实例成员变量有screen_size、price、brand
2)给成员变量创建 访问及设置 方法
3)定义play方法,功能为打印:play game
4)定义sendMessage方法,功能为打印:text message
5)定义powerOff方法,功能为打印:power off
6)创建get_info方法,打印对象的相关信息
7)实例化类phone(),对象名为phone1, 屏幕尺寸为5.5,价格为6288,品牌为apple
8)调用3,4,5,6方法,查看结果
9)实例化类phone(),对象名为phone2, 屏幕尺寸为5,价格为1999,品牌为mi
10)调用3,4,5,6方法,查看结果
class phone(object):
    
    def __init__(self,screen_size,price,brand):
        self.screen_size=screen_size
        self.price=price
        self.brand=brand
        self.pnum=100
    
    @property
    def play(self):
        return "play game"
    @classmethod    
    def sendMessage(cls,):
        return "text message"
        
    def powerOff(self,pnum):
        if pnum>10:
            return 'the left power is %s' % pnum
        else:
            return 'power off'
    
    def get_info(self,pnum):
        pnum=phone.powerOff(self,pnum)
        return 'screen_size: %s ,price: %s , brand: %s , powerleft: %s '% (self.screen_size,self.price,self.brand,pnum)  

phone1=phone(5.5, 6288, 'apple')
print(phone1.play)
print(phone.sendMessage())
print(phone1.powerOff(50))
print(phone1.get_info(50))
print('_____________________')
phone1=phone(5, 1999, 'mi')
print(phone1.play)
print(phone.sendMessage())
print(phone1.powerOff(5))
print(phone1.get_info(5))


题目2:完成简单类的设计
设计以下公司类,并实例化不同对象进行检查
类成员:类下公司的总个数
类方法:返回公司类共有多少个公司实例
实例变量:公司名,简介,利润,销售额,总成本,雇员姓名,雇员人员
实例方法要求有:
招聘人才(每招一个人会有成本产生,影响雇员列表,人数,总成本)
解雇人员(每解雇一个人会有成本产生,影响雇员列表,人数 ,总成本)
公司广告推广(影响总成本)
交社保(按公司雇员总人数计算,影响总成本)
交税(按公司雇员总人数计算,影响总成本)
销售(按销售件数价格计算销售额,利润按销售额利润率进行计算利润)
获取公司雇员列表
获取公司净利润
class company(object):
    company_num=0   #实例数
    company_list=[]   #公司名录
    def __init__(self,name,introduce,profit,salemoney,totalcost,namelist,personnum):
        self.name=name
        self.introduce=introduce
        self.profit=profit
        self.salemoney=salemoney
        self.totalcost=totalcost
        self.namelist=namelist   #雇员姓名
        self.personnum=personnum
        company.company_num+=1
        company.company_list.append(self.name)
    @classmethod
    def companynum(cls,):   #共有多少个公司实例
        return "company_num: %s,company_list: %s" % (company.company_num,company.company_list)
    def recruit(self,namer,costr):   #招聘人才(姓名,招聘成本)
        self.namelist.append(namer)
        self.personnum=self.personnum+1
        self.totalcost=self.totalcost-costr
        return "employee_list: %s, personnum: %s, totalcost: %s" % (self.namelist,self.personnum,self.totalcost)
    def fire(self,namef,costf):   #解雇人才(姓名,解雇成本)
        if len(self.namelist)==0:
            return 'the company has no employee'
        else:
            self.namelist.remove(namef)
            self.personnum=self.personnum-1
            self.totalcost=self.totalcost-costf
            return "employee_list: %s, personnum: %s, totalcost: %s" % (self.namelist,self.personnum,self.totalcost)
    def advertise(self,cost):   #公司广告(广告费)
        self.totalcost=self.totalcost-cost
        return "totalcost: %s" % self.totalcost
    def insurance(self,insmoney):   #交社保(人均保险费)
        self.totalcost=self.totalcost+self.personnum*insmoney
        return "totalcost: %s" % self.totalcost
    def tax(self,taxmoney):   #交税(人均税费)
        self.totalcost=self.totalcost+self.personnum*taxmoney
        return "totalcost: %s" % self.totalcost
    def sale(self,salenum):   #销售数
        self.salemoney=self.salemoney+salenum*100   #假设单价100
        return "salemoney: %s" % self.salemoney
    @property
    def getnamelist(self):   #获取雇员列表
        return "employeename: %s" % self.namelist
    @property 
    def getprofit(self):   #获取公司净利润
        return "companyprofit: %f" % float(self.profit+self.salemoney-self.totalcost)

C1=company('Baidu','Int',9999999999,88888888,222222222,['No.1','No.2','No.3','No.4','No.5','No.N'],50000)
print(company.companynum())
print(C1.recruit('No.ss',10000))
print(C1.fire('No.ss',2000))
print(C1.advertise(5000))
print(C1.insurance(1500))
print(C1.tax(1800))
print(C1.sale(100000))
print(C1.getnamelist)
print(C1.getprofit)


题目3:设计类及其继承类
设计一个叫cinema的电影院的类,包含:
类方法:(1)初始化方法,并初始相应的类变量实例变量(2)getSales(获取全部电影院实际销售的方法)的电影院
实例方法:saleTickets方法,要求更新实例的销售总额及类的销售总额
类变量:类下所有实例电影院的数量,销售额总和
实例变量:电影院名称,位置,销售额
创建一个miniCinema迷你电影院的类,继承自cinema类:
重写卖票方法,大于100元的票价加入打9折扣的功能
对miniCinema实例进行调用体现多态性
class cinema(object):
    cinema_list=[]   #电影院名字
    cinema_num=0   #电影院的数量
    cinema_sale=[]   #每个电影院销售额信息汇总
    totalsale=0   #销售额总和
    def __init__(self,name,position,salemoney):   #电影院名称,位置,销售额
        self.name=name
        self.position=position
        self.salemoney=salemoney
        cinema.cinema_list.append(name)
        cinema.cinema_num +=1
        cinema.cinema_sale.append(self.salemoney)
        cinema.totalsale +=self.salemoney
    def saleTickets(self,num,money):   #更新实例的销售总额及类的销售总额
        self.salemoney=self.salemoney+num*money   
        cinema.totalsale=cinema.totalsale+num*money
        return "totalsale: %s , salemoney: %s" % (cinema.totalsale,self.salemoney)
    @classmethod
    def getSales(cls,):   #获取全部电影院实际销售额对应
        cinema_sale_info={}
        for i in range(len(cinema.cinema_list)):
            csik=cinema.cinema_list[i]
            cinema_sale_info[csik]=cinema.cinema_sale[i]
        return "cinema_sale_info: %s" % cinema_sale_info

class miniCinema(cinema):
      def saleTickets(self,num,money):
            if money>100:
                money *=0.9
            else:
                pass
            self.salemoney=self.salemoney+num*money
            cinema.totalsale=cinema.totalsale+num*money
            return "totalsale: %s , salemoney: %s" % (cinema.totalsale,self.salemoney)  

cm1=cinema('Shidai','Haidian',20000)
print(cm.saleTickets(120,160))
cm2=cinema('Wanda','Dongcheng',40000)
print(cm.saleTickets(150,195))

micm=miniCinema('Douban','Internet',3000)
print(micm.saleTickets(100,100))
print(cinema.getSales())


题目4:导入包完成遍历目录
导入os包
创建递归循环完成一个目录的遍历
如果下还有一个目录,依次进行遍历
import os
def dircurrent(getcwd0):
    root=[getcwd0]
    listdir0=os.listdir(getcwd0)   #获取当前目录下文件
    for i in listdir0:
        if '.' not in i:
            getcwd1=getcwd0+'\\'+i
            root.append(getcwd1)
            print(root[-1])
            dircurrent(getcwd1)
        else:
            pass

getcwd0=os.getcwd()   #获取当前工作目录
dircurrent(getcwd0) 


1111111111111111.png


root1.png


root2.png


题目5:练习使用PIL包
导入PIL包
如果没有该包,请自行安装后导入
将一张图片进行尺寸缩放,颜色调整后保存(dir,help函数查看库)

julypython.png

from PIL import Image
pil_im=Image.open(r'julypython.png')
pil_im.thumbnail((255,255))
pil_im.save(r'julypython_change.png')
im=array(Image.open('julypython_change.png').convert('L'))
imshow(im)


julypython_change.png


1111111111111111.png


题目6:新建类并修改其实例的切片访问
Python中对序列的切片访问,是左闭右开
现要求:
创建一个可被迭代的类
改变默认的切片访问方式为左闭右闭
举例:c1=youclassname([0,1,2,3,4,5,6,7,8,9]
c1[2:7] 返回2,3,4,5,6,7
class newcut(object):
    def __init__(self,inter):
        self.inter=inter
    def getvalue(self,num1,num2):
        if (isinstance(c1.inter,str))|(isinstance(c1.inter,tuple))|(isinstance(c1.inter,list)):
            if len(self.inter)>num2:
                return list(self.inter[num1:num2])+[self.inter[num2]]
            else:
                return "num2 is bigger than the inter length"
        else:
            return "input object is not inter"

c1=newcut([0,1,2,3,4])
c1.getvalue(1,3)
已邀请:

机器龙007

赞同来自:


上面1,2,6题代码超边界没显示。。。。

题目1:
class phone(object):
    
    def __init__(self,screen_size,price,brand):
        self.screen_size=screen_size
        self.price=price
        self.brand=brand
        self.pnum=100
    
    @property
    def play(self):
        return "play game"
    @classmethod    
    def sendMessage(cls,):
        return "text message"
        
    def powerOff(self,pnum):
        if pnum>10:
            return 'the left power is %s' % pnum
        else:
            return 'power off'
    
    def get_info(self,pnum):
        pnum=phone.powerOff(self,pnum)
        return 'screen_size: %s ,price: %s , brand: %s , powerleft: %s '\
    % (self.screen_size,self.price,self.brand,pnum)    


题目2:
class company(object):
    company_num=0   #实例数
    company_list=[]   #公司名录
    def __init__(self,name,introduce,profit,salemoney,totalcost,namelist,personnum):
        self.name=name
        self.introduce=introduce
        self.profit=profit
        self.salemoney=salemoney
        self.totalcost=totalcost
        self.namelist=namelist   #雇员姓名
        self.personnum=personnum
        company.company_num+=1
        company.company_list.append(self.name)
    @classmethod
    def companynum(cls,):   #共有多少个公司实例
        return "company_num: %s,company_list: %s" \
    % (company.company_num,company.company_list)
    def recruit(self,namer,costr):   #招聘人才(姓名,招聘成本)
        self.namelist.append(namer)
        self.personnum=self.personnum+1
        self.totalcost=self.totalcost-costr
        return "employee_list: %s, personnum: %s, totalcost: %s" \
    % (self.namelist,self.personnum,self.totalcost)
    def fire(self,namef,costf):   #解雇人才(姓名,解雇成本)
        if len(self.namelist)==0:
            return 'the company has no employee'
        else:
            self.namelist.remove(namef)
            self.personnum=self.personnum-1
            self.totalcost=self.totalcost-costf
            return "employee_list: %s, personnum: %s, totalcost: %s" \
        % (self.namelist,self.personnum,self.totalcost)
    def advertise(self,cost):   #公司广告(广告费)
        self.totalcost=self.totalcost-cost
        return "totalcost: %s" % self.totalcost
    def insurance(self,insmoney):   #交社保(人均保险费)
        self.totalcost=self.totalcost+self.personnum*insmoney
        return "totalcost: %s" % self.totalcost
    def tax(self,taxmoney):   #交税(人均税费)
        self.totalcost=self.totalcost+self.personnum*taxmoney
        return "totalcost: %s" % self.totalcost
    def sale(self,salenum):   #销售数
        self.salemoney=self.salemoney+salenum*100   #假设单价100
        return "salemoney: %s" % self.salemoney
    @property
    def getnamelist(self):   #获取雇员列表
        return "employeename: %s" % self.namelist
    @property 
    def getprofit(self):   #获取公司净利润
        return "companyprofit: %f" % float(self.profit+self.salemoney-self.totalcost)

C1=company('Baidu','Int',9999999999,88888888,222222222,\
           ['No.1','No.2','No.3','No.4','No.5','No.N'],50000)
print(company.companynum())
print(C1.recruit('No.ss',10000))
print(C1.fire('No.ss',2000))
print(C1.advertise(5000))
print(C1.insurance(1500))
print(C1.tax(1800))
print(C1.sale(100000))
print(C1.getnamelist)
print(C1.getprofit)


题目6:
class newcut(object):
    def __init__(self,inter):
        self.inter=inter
    def getvalue(self,num1,num2):
        if (isinstance(c1.inter,str))\
        |(isinstance(c1.inter,tuple))\
        |(isinstance(c1.inter,list)):
            if len(self.inter)>num2:
                return list(self.inter[num1:num2])+[self.inter[num2]]
            else:
                return "num2 is bigger than the inter length"
        else:
            return "input object is not inter"

要回复问题请先登录注册

返回顶部