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