Python类和对象编程练习
第⼀题
创建⼀个名为User 的类,包含属性first_name 和last_name ,还有⽤户简介通常会存储的其他⼏个属性。在类User 中定义⼀个名为describe_user() 的⽅法,它打印⽤户信息摘要;再定义⼀个名为greet_user() 的⽅法,它向⽤户发出个性化的问候。创建多个表⽰不同⽤户的实例,并对每个实例都调⽤上述两个⽅法。
1)、如果类⾥⾯有初始化参数,创建实例的时候,必须在实例⾥⾯传递对应个数的参数。
nginx server配置
2)、调⽤函数的时候实例调⽤请复习下类函数,静态函数,实例函数。
class User:
def__init__(self,first_name,last_name,tel,place):
self.first_name=first_name
self.last_name=last_name
self.place=place
def describe_user(self):
print(self.last_name+self.first_name+"的电话号码{},地址{}".l,self.place))
def greet_user(self,content):
print(self.last_name+self.first_name+content)
u1=User("四","李","135671255","Beijing")
u1.describe_user()
<_user("你今天真好看")
第⼆题
定义⼀个学⽣类
1.有下⾯的类属性:姓名 年龄 成绩(语⽂,数学,英语)[每课成绩的类型为整数]
2.类⽅法
1) 获取学⽣的姓名:get_name() 返回类型:str
2 )获取学⽣的年龄:get_age() 返回类型:int
3 )返回3门科⽬中最⾼的分数:get_course() 返回类型:int
class Student:
def__init__(self,name,age,score):
self.name=name
self.age=age
self.score=score
def get_name(self):
return(str(self.name))儿童python入门教程
def get_age(self):
return(int(self.age))
def get_course(self):
return int(max(self.score))
Stu=Student("张三",20,[69,83,71])
_name())
_age())
_course())
第三题
按照以下要求定义⼀个游乐园门票类,并创建实例调⽤函数,完成⼉童和⼤⼈的总票价统计(⼈数不定,由你输⼊的⼈数个数来决定)
1)平⽇票价100元
2)周末票价为平⽇票价120%
3)⼉童半价
keyboard k2class Ticket:
def__init__(self,weekend=False,child=False):
if weekend:
self.inc=1.2
else:
self.inc=1
if child:
self.discount=0.5
else:
self.discount=1
def calcPrince(self,num):
p*self.inc*self.discount*num
return P
adult=Ticket()
child=Ticket(child=True)
print("2个成⼈+1⼀个⼩孩平⽇票价为:%.2f"%(adult.calcPrince(2)+child.calcPrince(1)))
class Ticket:
def__init__(self,price=100):
self.price=price
def ticket_price(self):
Day=int(input("请输⼊⽇期1-5代表⼯作⽇6-7代表周末:"))
Adult=int(input("请输⼊成年⼈⼈数:"))
Child=int(input("请输⼊⼉童⼈数:"))
if Day in range(1,6):
total_price=Adult*self.price+Child*self.price*0.5
elif Day in range(6,8):
total_price=Adult*self.price*1.2+Child*self.price*0.5*1.2
else:
print("输⼊错误,请重新输⼊")
return total_price
Price=Ticket().ticket_price()
print("需要⽀付{}元".format(Price))
第四题
⼈和机器猜拳游戏写成⼀个类,有如下⼏个函数:
1)函数1:选择⾓⾊ 1 曹操 2张飞 3 刘备
2)函数2:⾓⾊猜拳 1剪⼑ 2⽯头 3布 玩家输⼊⼀个1-3的数字
3)函数3:电脑出拳 随机产⽣1个1-3的数字,提⽰电脑出拳结果
4)函数4:⾓⾊和机器出拳对战,对战结束后,最后出⽰本局对战结果…赢…输,然后提⽰⽤户是否继续?按y继续,按n退出。
5)最后结束的时候输出结果 ⾓⾊赢⼏局 电脑赢⼏局,平局⼏次 游戏结束
import random
class Man_machine_war:
role_dict={1:"曹操",2:"张飞",3:"刘备"}
punch_dict={1:"剪⼑",2:"⽯头",3:"布"}
def get_role_name(self):#获取⾓⾊名
role_num=int(input("请选择⼀个⾓⾊:1 曹操 2张飞 3 刘备"))
le_dict[int(role_num)]
def get_role_punch(self):#⾓⾊出拳
punch_num=input("请出拳:1剪⼑ 2⽯头 3布请输⼊⼀个1-3的数字")
# return self.punch_dict[int(punch_num)]
return int(punch_num)
def get_computer_punch(self):#电脑出拳
punch_num=random.randint(1,3)
print("电脑出拳:{}".format(punch_num))
#return self.punch_dict[int(punch_num)]
return int(punch_num)
def games(self):#r⼈机对战
role_win =0# ⾓⾊赢
cp_win =0# 电脑赢
draw =0# 平局
role_name = _role_name()# 获取⾓⾊名
while True:
role__role_punch()#⾓⾊出拳
computer__computer_punch()#电脑出拳
print(role_name+"出拳为{},电脑出拳为{}".format(self.punch_dict[role_punsh],self.punch_dict[computer_punsh])) if role_punsh-computer_punsh==1or role_punsh-computer_punsh==-2:
role_win+=1#⾓⾊赢了
print("⾓⾊赢了")
elif role_punsh-computer_punsh==-1or role_punsh-computer_punsh==2:
excel教程视频2010
cp_win+=1#电脑赢了
print("电脑赢了")
elif role_punsh-computer_punsh==0:
draw +=1# 平局
print("平局")
choose=input("您是否要继续?按y继续,按n退出")
if choose=="n":
break
# return role_win,cp_win,draw#返回输赢
print("⾓⾊赢了{}局,电脑赢了{}局,平局{}".format(role_win, cp_win, draw))
if __name__=="__main__":
Man_machine_war().games()
第五题
按以下要求定义⼀个乌龟类和鱼类并尝试编写游戏
假设游戏场景范围(x,y)为0<=x<=10,0<=y<=10;
游戏⽣成1只乌龟和10条鱼;
它们的移动⽅向均随机;
乌龟最⼤移动能⼒是2(它可以随机选择1还是2移动),鱼⼉最⼤移动能⼒是1;
当移动到场景边缘,⾃动向反⽅向移动;
乌龟初始化体⼒为100(上限);
乌龟每移动⼀次,体⼒消耗1;
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体⼒增加20;
鱼暂不计算体⼒;
当乌龟体⼒值为0(挂掉)或者鱼⼉的数量为0时游戏结束;
import random as r
legal_y =[0,10]
class Turtle:
def__init__(self):
# 初始体⼒
self.power =100
# 初始位置随机
self.x = r.randint(legal_x[0],legal_x[1])
self.y = r.randint(legal_y[0],legal_y[1])
def move(self):
# 随机计算⽅向并移动到新位置(x,y)
new_x = self.x + r.choice([1,2,-1,-2])
new_y = self.x + r.choice([1,2,-1,-2])
# 检查移动后是否超出场景X轴边界
if new_x < legal_x[0]:
self.x = legal_x[0]-(new_x - legal_x[0])
elif new_x > legal_x[1]:
self.x = legal_x[1]-(new_x - legal_x[1])# 这⾥相当于⽤边界减去超出的距离差else:
self.x = new_x
# 检查移动后是否超出场景y轴边界
if new_y < legal_y[0]:
self.y = legal_y[0]-(new_y - legal_y[0])
elif new_y > legal_y[1]:
self.y = legal_y[1]-(new_y - legal_y[1])
else:
self.y = new_y
# 体⼒消耗
self.power -=1
# 返回移动后的新位置
return(self.x,self.y)
def eat(self):
self.power +=20
if self.power >100:
self.power =100
class Fish:
def__init__(self):
self.x = r.randint(legal_x[0],legal_x[1])
self.y = r.randint(legal_y[0],legal_y[1])
def move(self):
# 随机计算⽅向并移动到新位置(x,y)
new_x = self.x + r.choice([1,-1])
new_y = self.y + r.choice([1,-1])
# 检查移动后是否超出场景x轴边界
if new_x < legal_x[0]:
self.x = legal_x[0]-(new_x - legal_x[0])
elif new_x > legal_x[1]:
self.x = legal_x[1]-(new_y - legal_y[1])
else:
self.x = new_x
程序命名规则# 检查移动后是否超出场景y轴边界
if new_y < legal_y[0]:
self.y = legal_y[0]-(new_y - legal_y[0])
elif new_y > legal_y[1]:
self.y = legal_y[1]-(new_y - legal_y[0])
else:
self.y = new_y
# 返回移动后的位置
return(self.x,self.y)
fish =[]
for i in range(10):
new_fish = Fish()特殊文字乱码可复制
fish.append(new_fish)
while True:
if not len(fish):
print('鱼⼉都被吃完了,游戏结束!')
break
if not turtle.power:
print('乌龟体⼒耗尽,挂掉了!')
break
pos = ve()
# 在迭代器中删除列表元素是⾮常危险的,经常会出现意想不到的问题,因为迭代器是直接引⽤列表的数据进⾏引⽤# 这⾥我们把列表拷贝给迭代器,然后对原列表进⾏删除操作就不会有问题了!
for each_fish in fish[:]:
if ve()== pos:
# 鱼⼉被吃掉了
turtle.eat()
print('有⼀条鱼⼉被吃掉了.....')

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。