2.5-2.8
class CreditCard(): #2.5-2.9
def __init__(self,customer,bank,acnt,limit,balance = 0):
self._customer = customer
self._bank = bank
self._account = acnt
self._limit = limit
self._balance = balance
def get_customer(self):
return self._customer
def get_bank(self):
self._bank
def get_account(self):
return self._account
def get_limit(self):
return self._limit
def get_balance(self):
return self._balance
def charge(self,price):
try:
if price + self._balance > self._limit:
return False
else:
self._balance += price
return True
except TypeError:
print('请输入数字')
def make_payment(self,amount):
if amount >= 0:
self._balance = amount
else:
raise ValueError
if __name__ == '__main__':
wallet = []
wallet.append(CreditCard('John Bowman', 'California Savings',
'5391 0375 9387 5309', 2500))
wallet.append(CreditCard('John Bowman', 'California Federal',
'3485 0399 3395 1954', 3500))
wallet.append(CreditCard('John Bowman', 'California Finance',
'5391 0375 9387 5309', 5000))
for val in range(1, 17):
wallet[0].charge(val)
wallet[1].charge(2 * val)
wallet[2].charge(3 * val)
for c in range(3):
print('Customer =', wallet[c].get_customer())
print('Bank =', wallet[c].get_bank())
print('Account =', wallet[c].get_account())
print('Limit =', wallet[c].get_limit())
print('Balance =', wallet[c].get_balance())
while wallet[c].get_balance() > 100:
wallet[c].make_payment(100)
print('New balance =', wallet[c].get_balance())
def __init__(self,name = 0,num = 0,price = 0):
self.name = name
self.num = num
self.price = prio
2.9-2.15
import copy
class Vector():
def __init__(self,d): #2.15
if isinstance(d,list):
self._coords = copy.deepcopy(d)
else:
self._coords = [0] * d
def __len__(self):
return len(self._coords)
def __getitem__(self,j):
return self._coords[j]
def __setitem__(self,j,val):
self._coords[j] = val
def __add__(self,other):
if len(self) != len(other):
raise ValueError ('长度必须一致')
result = Vector(len(self))
for i in range(len(self)):
result[i] = self[i] +other[i]
return result
def __sub__(self,other): #2.9
if len(self) != len(other):
raise ValueError ('长度必须一致')
result = Vector(len(self))
for i in range(len(self)):
result[i] = self[i] - other[i]
return result
def __neg__(self): #2.10
result = Vector(len(self))
for i in range(len(self)):
if self[i] >= 0:
result[i] = -self[i]
else:
result[i] = self[i]
return result
# def __mul__(self,other): #2.12
# result = Vector(len(self))
# for i in range(len(self)):
# result[i] = self[i] * other
# return result
def __rmul__(self,other): #2.13
result = Vector(len(self))
for i in range(len(self)):
result[i] = self[i] * other
return result
def __radd__(self,other): #2.11
if len(self) != len(other):
raise ValueError ('长度必须一致')
result = Vector(len(self))
for i in range(len(self)):
result[i] = self[i] +other[i]
return result
def __mul__(self,other): #2.14
result = 0
for i in range(len(self)):
result += self[i] * other[i]
return result
def __eq__(self,other):
return self._coords == other._coords
def __ne__(self,other):
return not self == other
def __str__(self):
return '<' + str(self._coords)[1:-1] + '>'
__repr__ = __str__ #直接输入实例名即可显示该实例的坐标,无需print方法
2.16
2.17(复习ing,随后更)