活跃农民
- 积分
- 584
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-4-1
- 最后登录
- 1970-1-1
|
disc06并不难嘛 ,上代码:
#Disc06
#Q2.1_2
def take_color(self, color):
for i in range(len(skittles)):
color_skittle = self.skittles[i].color
if color_skittle == color:
return self.skittles.pop(i)
return None
#Q2.1_3
def take_all(self):
for _ in range(len(self.skittles)):
print(self.take_skittle().color)
#3.2_1
class Cat(Pet):
def __init__(self, name, owner, lives=9):
Pet.__init__(self, name, owner)
self.lives = lives
def talk(self):
print('meow!')
def lose_life(self):
if self.lives > 0:
self.lives -= 1
if self.lives == 0:
self.is_alive = False
else:
print(str(self.name) + " is dead.")
#Q4.2_1
def __neg__(self):
return Vector([-i for i in self.vector])
#Q4.2_2
def __add_(self, other):
assert type(other) == Vector, "Invalid operation!"
assert len(self) == len(other), "Invalid dimensions!"
return Vector([self.vector[i] + other.vector[i] for i in range(len(self))])
#Q4.2_3
def __mul__(self, other):
if type(other) == int or type(other) == float:
return Vector([self.vector[i] * other for i in range(len(self))])
elif type(other) == Vector:
assert len(self) == len(other), "Invalid dimensions!"
return sum([self[n] * other[n] for n in range(len(self))]) |
|