Exercise
Violence motorcycle program (complete the following requirements):
Create three game characters, which are:
Aoi well, female, 18, attack ad is 20, 200 blood
Tony Wood, male, 20, attack ad 30, 150 blood
A lot of waves, female, 19, 50 attack ad, blood 80
Create three games weapon, namely:
Pans, ad 20
Ax, ad 50
Nunchaku, ad 65
Create three games motorcycle, namely:
Small pedal speed step 60
Yamaha, speed step 80
BMW, 120 speed step.
Complete the following requirements (use of weapons as a weapon to beat off the blood of human ad + ad):
(1) Aoi driving pedal shaft 60 riding on the car with the step on the track.
(2) driving a BMW 120 Tony Wood rode a step of driving the car on the track.
(3) a lot of wave riding step 80 is open Yamaha vehicle traveling on the track.
(4) Cang wave hit wells lot 20 unarmed blood, blood xx left a lot of waves.
(5) Wood Tony wave hit lot 30 unarmed blood, blood xx left a lot of waves.
(6) Boduoduoli pan hit a pan well Aoi, Aoi well xx left blood.
(7) Boduoduoli ax ax hit a Tony Wood, Anthony Wood left xx blood.
(8) Aoi well riding the BMW hit rode a small pedal Tony Wood nunchakus, Tony Wood cried, left xx blood. (OPTIONAL)
(9) riding on a lot of wave riding pedal playing a Yamaha ax Tony Wood, Anthony Wood crying, left xx blood. (OPTIONAL)
class Role:
def __init__(self,name,sex,age,ad,blood):
self.name = name
self.sex = sex
self.age = age
self.ad = ad
self.blood = blood
def fight(self, beaten):
beaten.blood -= self.ad
print(f"{self.name}赤手空拳大了{beaten.name}{self.ad}滴血,{beaten.name}还剩下{beaten.blood}")
def fight2(self,beaten):
beaten.blood -= self.ad + self.weapon.ad
print(f"{self.name}骑着{self.motorcycle.name}打了骑着{beaten.motorcycle.name}的{beaten.name}一{self.weapon.name},{beaten.name}哭了,还剩{beaten.blood}")
class Weapon:
def __init__(self,name,ad):
self.name = name
self.ad = ad
def finght(self,p1,p2):
p2.blood -= p1.ad +self.ad
print(f"{p1.name}利用{self.name}打了{p2.name}一{self.name},{p2.name}还剩{p2.blood}血")
class Motorcycle:
def __init__(self,name,speed):
self.name = name
self.speed = speed
def drving(self,who):
print(f"{who.name}骑着{self.name}开着{self.speed}迈的车行驶在赛道上.")
cjj = Role("苍井井","女",18,20,200)
dnmm = Role("东尼木木","男",20,30,150)
bdd = Role("波多多","女",19,50,80)
pdg = Weapon("平底锅",20)
fz = Weapon("斧子",50)
sjg = Weapon("双截棍",65)
xtb = Motorcycle("小踏板",60)
ymh = Motorcycle("雅马哈",80)
bm = Motorcycle("宝马",120)
cjj.motorcycle = xtb
dnmm.motorcycle = bm
bdd.motorcycle = ymh
cjj.motorcycle.drving(cjj)
dnmm.motorcycle.drving(dnmm)
bdd.motorcycle.drving(bdd)
cjj.fight(bdd)
dnmm.fight(bdd)
bdd.weapon = pdg
bdd.weapon.finght(bdd,cjj)
bdd.weapon = fz
bdd.weapon.finght(bdd,dnmm)
cjj.motorcycle = bm
cjj.weapon = sjg
dnmm.motorcycle = xtb
cjj.fight2(dnmm)
bdd.motorcycle = xtb
bdd.weapon = fz
dnmm.motorcycle =ymh
bdd.fight2(dnmm)
Define a class, calculating the perimeter and the area of a circle.
class Circular: pi = 3.14159 def __init__(self,radius): self.radius =radius def girth(self): print(Circular.pi*self.radius*2) def area(self): print(Circular.pi*self.radius**2) s = Circular(3) s.girth() s.area()
Class defines a ring, the ring perimeter and the area is calculated (title upgrade).
class Ring: pi = 3.14159 def __init__(self, radius,radius1): self.radius = radius self.radius1 = radius1 def girth(self): print(Ring.pi * self.radius * 2+Ring.pi * self.radius1 * 2) def area(self): print(abs(Ring.pi * self.radius ** 2-Ring.pi * self.radius1 ** 2))
The definition of a school class, a class teacher.
- School class requirements:
- School class encapsulates school name, school address, and related properties teacher (teacher objects stored in a list) of.
- name: name of the school.
- address: specific address.
- teacher_list: []。
- School class teacher set the method to add an object.
- School class encapsulates school name, school address, and related properties teacher (teacher objects stored in a list) of.
- Teacher class package name, professor of disciplines, as well as the specific object belongs to the school.
- name: the name of the teacher.
- course: discipline.
- school: school specific object.
- Examples of two campuses:
- Beijing campus, the beautiful River;
- Campus Shenzhen, Nanshan District.
- Examples of six teacher:
- Bai, Python, Beijing campus objects.
- Wu Chao, linux, Beijing campus objects.
- Bao Yuan, python, Beijing campus objects.
- Yuan Hao, python, Shenzhen campus objects.
- Tigers, linux, Shenzhen campus objects.
- Wang, Python, Shenzhen campus objects.
- Complete the following specific requirements:
- Bai obtain their schools name.
- Bai obtain their schools school address.
- All the teachers were all part of Beijing campus displayed, and add to a list.
- All teachers belong to all campuses and disciplines Shenzhen is responsible displayed.
- Add all the teachers in charge of Python objects subject to a two campuses of the list.
- Add the object is responsible for all teachers linux subject to a list of two campuses and recycled displayed.
- The Beijing campus of the object using the pickle written to the file, and then read out and demonstrate the Beijing campus of the teacher's name belongs
class School:
def __init__(self,name,address):
self.name = name
self.address =address
self.teacher_list=[]
def recruit(self,teacher):
self.teacher_list.append(teacher)
# teacher.school = self
class Teacher:
def __init__(self,name,course,school):
self.name = name
self.course = course
self.school = school
self.school.recruit(self)
bj = School("北京校区","美丽的沙河")
sz = School("深圳校区","南山区")
taibai = Teacher("太白","python",bj)
wuchao= Teacher("吴超","linux",bj)
baoyuan = Teacher("宝元","python",bj)
yuanhao = Teacher("苑昊","python",sz)
xiaohu = Teacher("小虎","linux",sz)
xiaowang = Teacher("小王","python",sz)
print(taibai.school.name)
print(taibai.school.address)
lst = []
for i in bj.teacher_list:
print(i.name)
lst.append(i.name)
print(lst)
for i in sz.teacher_list:
print(F"{i.name}负责{i.course}")
lst1 = list(filter(lambda x:x.course=="python" ,bj.teacher_list))+list(filter(lambda x:x.course=="python" ,sz.teacher_list))
for i in lst1:
print(i.name)
lst2 = list(filter(lambda x:x.course=="linux" ,bj.teacher_list))+list(filter(lambda x:x.course=="linux" ,sz.teacher_list))
for i in lst2:
print(i)
import pickle
# pickle.dump(bj,open("txt","wb"))
l = pickle.load(open("txt","rb"))
for i in l.teacher_list:
print(i.name)