2022年3月3日

Python: 类 对象 属性

class A(object): def __init__(self): self.var1 = 1 class B(object): def __init__(self, arg1): self.var2 = arg1 self.var3 = A() x = B(2) y = x.var3.var1 print(type(x)) #x是B类对象 print(type(x.var3)) #x.var3是A类对象 print(type(x.var3.var1)) #x.var3.var1是A类对象属性…
2022年3月3日

Python: 练习 魔术函数__add__(self,other)

class CellPhone: # 定义一个手机类的模板,包括品牌(brand)和价格(price) def __init__(self, brand, price=0.0): self.brand = brand self.price = price def __str__(self): return str(self.price) def __add__(self, other): # 魔术函数,self 指实例化对象本身,other 指另一个实例化对象本身. return CellPhone(brand='', price=(self.price + other.price)) # 将实例化对象的价格求和,得到两个品牌手机价格的和. c1 = CellPhone('iPhone 7', 7000.0) # 实例化对象1 c2 = CellPhone('MI note 2', 2000.0)…
2022年2月26日

Python: 练习 面向对象 搬家具

class Furniture: def __init__(self, name, area): self.name = name self.area = area class Room: def __init__(self, location, area): self.location = location self.area = area self.free_area = area self.furniture_list = [] def add_furniture(self, item): if item.area > self.free_area: print("房间空间不足,无法放置") else: self.furniture_list.append(item.name) self.free_area -= item.area def __str__(self): return f"…
2022年2月26日

Python: 练习 面向对象 烤地瓜

class SweetPotato: def __init__(self): self.cook_time = 0 self.state = "生的" self.flavour = [] def cook_program(self, time): self.cook_time += time if 0 <= self.cook_time < 3: self.state = "生的" elif 3 <= self.cook_time < 5: self.state = "半生" elif 5 <= self.cook_time < 8: self.state = "熟啦" elif self.cook_time >= 8: self.state = "糊了" def cook_flavour(self, flavour): self.flavour.append(flavour) d…
2022年2月23日

Python 练习:学员管理系统

# 学员管理系统 def welcome():     print("学员管理系统")     print("*" * 20)     print("1.增加学员 2.删除学员 3.修改学员 4.查询学员 5.显示所有学员 6.退出")     print("*" * 20)     input_num = int(input("请输入功能序号:"))     return input_num info = [] def get_stu_name():     stu_name = input("请输入学员姓名:")     return stu_name def add_stu_info():     stu_name = input('…
2022年2月14日

Python 第一个程序:石头,剪刀,布

import randomp = player = int(input("请出招(石头-0,剪刀-1,布-2):"))c = computer = random.randint(0, 2)# print(f"你:{p}")# print("电脑:%d" % c)# print(f"你:{p}\n电脑:{c}")print("你:%d\n电脑:%d" % (p, c))if (p == 0 and c == 1) or (p == 1 and c == 2) or (p == 2 and c == 0): print("You Win!")elif p == c: print("平局")else: print("You Lose!")…