Python面向对象练习题

1.模拟栈操作原理
  先进后出
  ​1.初始化(创建一个存储数据的列表)
  2.进栈使用列表保存数据
​  3.出栈 使用列表删除数据
​  4.查看栈顶元素  切片获取列表最后一位数据
  ​5.判断是否为空栈
  6.栈的长度

程序
stack=[]
info="""
********栈操作******
1.入栈
2.出栈
3.栈顶元素
4.栈的长度
5.栈是否为空
"""
while True:
    print(info)
    choice=input("please input your choice:")
    if choice=='1':
        item=input('请输入入栈元素:')
        stack.append(item)
        print('%s入栈成功!'%item)
    elif choice=='2':
        if  not stack:
            print('栈为空,不能出栈!')
        else:
            item=stack.pop()
            print('%s出栈成功!'%item)
    elif choice=='3':
        if len(stack)==0:
            print('栈为空!')
        else:
            print('栈顶元素为:%s'%stack[-1])
    elif choice=='4':
        print('栈的长度为%s'%len(stack))
    elif choice=='5':
        if len(stack)==0:
            print('栈为空!')
        else:
            print('栈不为空!')
    elif choice=='q':
        print('logout')
        break
    else:
        print('Error:check your input!')

  输出结果:

********栈操作******
1.入栈
2.出栈
3.栈顶元素
4.栈的长度
5.栈是否为空

please input your choice:1
请输入入栈元素:2
2入栈成功!

********栈操作******
1.入栈
2.出栈
3.栈顶元素
4.栈的长度
5.栈是否为空

please input your choice:

  

2.家具

1.房子有户型,总面积和家具名称列表,新房子没有任何的家具
2.家具有名字和占地面积,其中
  床: 占4平米   衣柜: 占2平面   餐桌: 占1.5平米 3.将以上三件家具添加到房子中 4.打印房子时,要求输出:户型,总面积,剩余面积,家具名称列表 分析: 1.由于要将家具放入房子中,所以需要先创建家具类 2.家具类: (1)属性:名字(name),占地面积(area) (2)对象:床(bed),衣柜(closet),餐桌(table) 3.房子类: (1)属性:户型(house_style),总面积(zarea), 家具名称列表(namelist) (新房子没有任何的家具,即初始家具名称列表为空列 表) 剩余面积(farea) (由于打印房子时,要求输出'剩余面积',所以剩余面积为房子 的隐含属性) (2)方法:添加家具(add_item)
class HouseItem:
    def __init__(self, name, area):
        self.name = name
        self.area = area
    def __str__(self):
        return "[%s] 占地 %.2f" % (self.name, self.area)

class House:
    def __init__(self, house_type, area):
        self.house_type = house_type
        self.area = area
        self.free_area = area
        self.item_list = []
    def __str__(self):
        # python能够自动的将一对括号内部的代码连接在一起
        return ("户型: %s\n总面积: %.2f[剩余: %.2f]\n家具:%s"
                %(self.house_type, self.area,
                  self.free_area, self.item_list))
    def add_item(self, item):
        print("要添加 %s" % item)
        if item.area > self.free_area:
            print("%s的面积太大了,无法添加" % item.name)
            return
        self.item_list.append(item.name)
        self.free_area -= item.area

bed = HouseItem("床", 4)
chest = HouseItem("衣柜", 2)
table = HouseItem("餐桌", 1.5)
print(bed)
print(chest)
print(table)

my_home = House("两室一厅", 60)
my_home.add_item(bed)
my_home.add_item(chest)
my_home.add_item(table)
print(my_home)

  

 

3.
​	1.士兵瑞恩有一把AK47
​	2.士兵可以开火(士兵开火扣动的是扳机)
​	3.枪 能够 发射子弹(把子弹发射出去)
​	4.枪 能够 装填子弹 --增加子弹的数量
​	Soldier                     Gun
-------------               -----------------
​	name                        model
​	gun                         bullet_count #子弹数量足够多才能完成射击的动作
-------------               -----------------
​	__init__(self):                 __init__(self):
​	fire(self):                 add_bullet(self,count):#装填子弹的
​								shoot(self):

	分析:
		1.由于士兵瑞恩有一把AK47,士兵可以开火。故需要先创建枪类
	
		2.枪类(Gun):
			 (1)属性:型号(model),子弹数目(bullet_count)
			 (2)方法:发射子弹(shoot),装填子弹(add_bullet)
		 
		3.士兵类(Soldier)
			 (1)属性:姓名(name),枪名(Gun)
			 (2)方法:开火(fire)

class Gun: #Gun枪
    def __init__(self, model):
        self.model = model
        self.bullet_count = 0
    def add_bullet(self, count):
        self.bullet_count += count
    def shoot(self):
        if self.bullet_count <= 0:
            print("[%s没有子弹了..." % self.model)
            return
        self.bullet_count -= 1
        print("[%s]突突突...[%d]" % (self.model, self.bullet_count))

class Soldier:
    def __init__(self,name):
        self.name = name
        self.Gun = Gun
    def fire(self):
        if self.Gun is None:
            print("[%s]还有枪..." % self.name)
            return
        print("冲啊炮灰们...[%s]" % self.name)
        self.Gun.add_bullet(50)
        self.Gun.shoot()


AK47 = Gun("AK47")
xusandou = Soldier("士兵")
xusandou.Gun = AK47
xusandou.fire()
print(xusandou.Gun)