1 class Account: 2 def __init__(self, name, account_number, initial_amount = 10): 3 self._name = name 4 self._card_no = account_number 5 self._balance = initial_amount 6 def deposit(self, amount): 7 self._balance += amount 8 def withdraw(self, amount): 9 if self._balance < amount: 10 print('余额不足') 11 return 12 13 self._balance -= amount 14 15 def info(self): 16 print('持卡人姓名:', self._name) 17 print('持卡人账号:', self._card_no) 18 print('持卡人账户余额:', self._balance) 19 def get_balance(self): 20 return self._balance 21 def main(): 22 print('测试账户1:'.center(30, '*')) 23 a1 = Account('Bob', '5002311', 20000) 24 a1.deposit(5000) 25 a1.withdraw(4000) 26 a1.info() 27 print() 28 print('测试账户2:'.center(30, '*')) 29 a2 = Account('Joe', '5006692', 20000) 30 a2.withdraw(10000) 31 a2.withdraw(5000) 32 a2.info() 33 if __name__ == '__main__': 34 main()
task 2
1 class Shape: 2 def info(self): 3 pass 4 def area(self): 5 pass 6 def preimeter(self): 7 pass 8 class Rect(Shape): 9 def __init__(self, x = 0, y = 0, length = 2, width = 1): 10 self._x = x 11 self._y = y 12 self._width = width 13 self._length = length 14 def info(self): 15 print(f'矩形左上角顶点坐标: ({self._x}, {self._y})') 16 print(f'矩形长: {self._length}') 17 print(f'矩形宽: {self._width}') 18 def area(self): 19 return self._length * self._width 20 def perimeter(self): 21 return (self._length + self._width) * 2 22 class Circle(Shape): 23 def __init__(self, x = 0, y = 0, radius = 1): 24 self._x = x 25 self._y = y 26 self._r = radius 27 def info(self): 28 print(f'圆心: ({self._x}, {self._y})') 29 print(f'半径: {self._r}') 30 def area(self): 31 return 3.14 * self._r * self._r 32 def perimeter(self): 33 return 2 * 3.14 * self._r 34 class Triangle(Shape): 35 def __init__(self, a = 1, b = 1, c = 1): 36 self._a, self._b, self._c = a, b, c 37 def info(self): 38 print(f'三角形三边长: ({self._a}, {self._b}, {self._c})') 39 def area(self): 40 s = (self._a + self._b + self._c) / 2 41 ans = (s*(s - self._a)*(s - self._b)*(s - self._c)) ** 0.5 42 return ans 43 def perimeter(self): 44 return (self._a + self._b + self._c) 45 def main(): 46 print('测试1:'.center(40, '*')) 47 shapes_lst1 = [Circle(), Rect(), Triangle()] 48 for t in shapes_lst1: 49 t.info() 50 print(f'面积: {t.area():.2f}') 51 print(f'周长: {t.perimeter():.2f}') 52 print() 53 print('测试2:'.center(40, '*')) 54 shapes_lst2 = [Circle(x = 2, y = 2, radius = 10), 55 Rect(x = 50, y = 50, length = 10, width = 5), 56 Triangle(a = 3, b = 4, c = 5)] 57 for t in shapes_lst2: 58 t.info() 59 print(f'面积: {t.area():.2f}') 60 print(f'周长: {t.perimeter():.2f}') 61 print() 62 if __name__ == '__main__': 63 main()
from shape import Rect, Circle shape_lst = [Rect(5, 5, 10, 5), Circle(), Circle(1, 1, 10)] for i in shape_lst: i.info() print(f'面积: {i.area(): .2f}') print(f'周长: {i.perimeter(): .2f}') print()
from math import * m = 0 s = 2 def func(x): return 1/(sqrt(2*pi)*s)*(exp((-0.5)*((x-m)/s)**2)) print(f'x = 1,f = {func(1):.8f}') print(f'x = 3,f = {func(3):.8f}') print(f'x = 5,f = {func(5):.8f}') print(f'x = 7,f = {func(7):.8f}') print(f'x = 9,f = {func(9):.8f}')
标签:__,info,self,实验,._,print,def From: https://www.cnblogs.com/songsongsong/p/17478657.html