Python object oriented exercises

Anyang 2021-04-07 16:31:57
python object oriented exercises


1. Operation principle of analog stack
First in, then out
​1. initialization ( Create a list of stored data )
2. Use the list to save data
​ 3. Out of the stack Use lists to delete data
​ 4. Look at the top of the stack elements Slice to get the last bit of the list
​5. Judge whether it is an empty stack
6. The length of the stack 

Program
stack=[]
info="""
******** Stack operation ******
1. Push
2. Out of the stack
3. Top element of stack
4. The length of the stack
5. Is the stack empty
"""
while True:
print(info)
choice=input("please input your choice:")
if choice=='1':
item=input(' Please enter the stack element :')
stack.append(item)
print('%s Stack successfully !'%item)
elif choice=='2':
if not stack:
print(' The stack is empty. , Cannot stack !')
else:
item=stack.pop()
print('%s Stack out successfully !'%item)
elif choice=='3':
if len(stack)==0:
print(' The stack is empty. !')
else:
print(' The element at the top of the stack is :%s'%stack[-1])
elif choice=='4':
print(' The length of the stack is %s'%len(stack))
elif choice=='5':
if len(stack)==0:
print(' The stack is empty. !')
else:
print(' Stack is not empty. !')
elif choice=='q':
print('logout')
break
else:
print('Error:check your input!')

Output results :

******** Stack operation ******
1. Push
2. Out of the stack
3. Top element of stack
4. The length of the stack
5. Is the stack empty
please input your choice:1
Please enter the stack element :2
2 Stack successfully !
******** Stack operation ******
1. Push
2. Out of the stack
3. Top element of stack
4. The length of the stack
5. Is the stack empty
please input your choice:

2. furniture 

1. There are different types of houses , List of total area and furniture names , The new house doesn't have any furniture
2. The furniture has a name and an area , among 
The bed : Occupy 4 Square meters Wardrobe : Occupy 2 Plane table : Occupy 1.5 Square meters 3. Add these three pieces of furniture to the house 4. When you print the house , Request output : House type , Total area , The remaining area , List of furniture names analysis : 1. Because of putting furniture in the house , So you need to create furniture class first 2. Furniture : (1) attribute : name (name), Covers an area of (area) (2) object : The bed (bed), Wardrobe (closet), table (table) 3. Houses : (1) attribute : House type (house_style), Total area (zarea), List of furniture names (namelist) ( The new house doesn't have any furniture , The initial furniture name list is empty surface ) The remaining area (farea) ( Because when printing houses , Request output ' The remaining area ', So the remaining area is the house The implied property of ) (2) Method : Add furniture (add_item)
class HouseItem:
def __init__(self, name, area):
self.name = name
self.area = area
def __str__(self):
return "[%s] Covers an area of %.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 Can automatically connect the code inside a pair of brackets together
return (" House type : %s\n Total area : %.2f[ The remaining : %.2f]\n furniture :%s"
%(self.house_type, self.area,
self.free_area, self.item_list))
def add_item(self, item):
print(" You want to add %s" % item)
if item.area > self.free_area:
print("%s It's too big , Unable to add " % item.name)
return
self.item_list.append(item.name)
self.free_area -= item.area
bed = HouseItem(" The bed ", 4)
chest = HouseItem(" Wardrobe ", 2)
table = HouseItem(" table ", 1.5)
print(bed)
print(chest)
print(table)
my_home = House(" two bedrooms ", 60)
my_home.add_item(bed)
my_home.add_item(chest)
my_home.add_item(table)
print(my_home)

 

3.
​ 1. Soldier Ryan has one AK47
​ 2. Soldiers can fire ( The soldiers fired and pulled the trigger )
​ 3. gun can bullets ( Shoot the bullet out )
​ 4. gun can Loading bullets -- Increase the number of bullets
​ Soldier Gun
------------- -----------------
​ name model
​ gun bullet_count # There are enough bullets to complete the shooting
------------- -----------------
​ __init__(self): __init__(self):
​ fire(self): add_bullet(self,count):# Loaded with bullets
​ shoot(self):
analysis :
1. Because soldier Ryan has one AK47, Soldiers can fire . So you need to create a gun class first
2. Guns (Gun):
(1) attribute : model (model), The number of bullets (bullet_count)
(2) Method : bullets (shoot), Loading bullets (add_bullet)
3. Soldiers (Soldier)
(1) attribute : full name (name), Gun name (Gun)
(2) Method : FireStarter (fire)

class Gun: #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 There are no more bullets ..." % self.model)
return
self.bullet_count -= 1
print("[%s] A sudden outburst ...[%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] And guns ..." % self.name)
return
print(" Go ahead, cannon fodder ...[%s]" % self.name)
self.Gun.add_bullet(50)
self.Gun.shoot()
AK47 = Gun("AK47")
xusandou = Soldier(" rank-and-file soldiers ")
xusandou.Gun = AK47
xusandou.fire()
print(xusandou.Gun)

 

 

 


 

版权声明
本文为[Anyang]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/04/20210407162500684q.html

  1. 用 Python 读写 Excel 表格
  2. Python装饰器高级用法
  3. Reading and writing excel table with Python
  4. Python decorator advanced usage
  5. 从零开始学python | 什么是Python JSON?
  6. Learn Python from scratch | what is Python JSON?
  7. Python with关键字原理详解
  8. Python with keyword principle
  9. python + uiautomator2 实现钉钉工单提交
  10. 一个神级般的 Python 调试神器
  11. Nail work order submission based on Python + uiautomator2
  12. A magic Python debugging artifact
  13. 从零开始学python | 什么是Python JSON?
  14. Learn Python from scratch | what is Python JSON?
  15. 上手Pandas,带你玩转数据(3)-- pandas数据存入文件
  16. Start pandas, take you to play with data (3) -- save pandas data into file
  17. Python能代替shell吗?有什么特点?
  18. Can Python replace shell? What are the features?
  19. Scikit-learn 机器学习库介绍!【Python入门】
  20. Scikit learn machine learning library introduction! [introduction to Python]
  21. 【Python从零到壹】面向对象的封装,继承和多态
  22. Scikit-learn 机器学习库介绍!【Python入门】
  23. Python能代替shell吗?有什么特点?
  24. 【Python从零到壹】Python文件的操作详解
  25. 【Python从零到壹】Python爬虫部分开篇
  26. 【Python从零到壹】学习Python爬虫前,你需要先掌握这些内容
  27. 【Python从零到壹】python爬虫系列-网络请求
  28. Object oriented encapsulation, inheritance and polymorphism
  29. Python经典面试题(附答案)!
  30. Scikit learn machine learning library introduction! [introduction to Python]
  31. 菜谱系统小成阶段,Python Web 领域终于攻占一个小山头
  32. Python Web 菜谱项目再次前进一步,从应用层了解内置用户认证系统
  33. Can Python replace shell? What are the features?
  34. Detailed explanation of Python file operation
  35. Python from zero to one
  36. [Python from zero to one] before learning Python crawler, you need to master these contents
  37. [Python from zero to one] Python crawler series - web request
  38. Python经典面试题(附答案)!
  39. Python classic interview questions (with answers)!
  40. When the recipe system was in its infancy, python web finally took over a small hill
  41. Python web menu project takes another step forward to learn about the built-in user authentication system from the application layer
  42. Python classic interview questions (with answers)!
  43. 【Python从零到壹】Python的循环结构详解
  44. 【Python从零到壹】Python列表详解
  45. 【Python从零到壹】Python的字典详解
  46. 【Python从零到壹】Python的字符串详解
  47. 【Python从零到壹】Python基础之函数的应用
  48. 【Python从零到壹】用Python实现植物大战僵尸里的面向对象
  49. Detailed explanation of Python loop structure
  50. Detailed explanation of Python list
  51. Detailed explanation of Python dictionary
  52. Detailed explanation of Python string
  53. [Python from zero to one] the application of Python basic functions
  54. [Python from zero to one] using Python to realize object-oriented in plant vs. zombie
  55. 用 Python 实现微信版飞机大战
  56. 用 Python 实现***帝国中的数字雨落既视感
  57. 想知道未来孩子长相?Python人脸融合告诉你
  58. 我用 Python 做了一个全球疫情数据大屏
  59. Using Python to realize wechat aircraft war
  60. Using Python to realize the visual sense of digital rain in the Empire of the Communist Party of China